#!/usr/bin/python
#coding: Utf-8
Import commands
Import OS
Import re
Import datetime
#if mem util > Echo to Mem_file_report and CPU util
Mem_info = Commands.getstatusoutput (' Top-b|head-n30|tail-n24 ')
info = mem_info[1].split (' \ n ')
Count = 0
num = 0
For I in info:
if Count = = 0:
Global Header
Header = I
If Count >= 1:
I_list = I.split ()
Num_mem = I_list[9].split ('. ') [0]
NUM_CPU = I_list[8].split ('. ') [0]
if int (NUM_MEM) >= or int (NUM_CPU) >= 50:
with open(‘/opt/scripts/mem_cpu_tcp_check.log‘,‘a‘) as f: result = "%s %s"%(datetime.datetime.now(),i) f.write(result + ‘\n‘) num+=1 if count >= 23 and num >= 1: with open(‘/opt/scripts/mem_cpu_tcp_check.log‘,‘a‘) as f: result1 = "%s %s"%(datetime.datetime.now(),header) f.write(‘\n‘ + result1 + ‘\n‘ + ‘=========================================================‘ + ‘\n‘)count+=1
#tcp Connection Details
Tcp_num = Commands.getstatusoutput ("Netstat-antupl|grep established|wc-l")
#print Tcp_num[1]
Tcp_info = Commands.getstatusoutput ("Netstat-antupl|grep established")
if Int (tcp_num[1]) >= 100:
With open ('/opt/scripts/tcp_check.log ', ' a ') as F:
f.write(‘\n‘ + "============================================================" + ‘\n‘ + str(datetime.datetime.now()) + ‘\n‘ + tcp_info[1])
Interface Liuliang Warning
Liuliang = Commands.getstatusoutput ("Iftop-i eth0-n-p-t-L 50-s 10")
#liuliang_list = Liuliang[1].split (' \ n ')
#print Liuliang[1]
#for I in Liuliang_list:
New_num = I.split () [4]NEW_NUM1 = Re.findall (R ' \d+ ', new_num) [0]print new_num1if int (new_num1) = = 0:liuliang_list.remove (i)
#print ' \ n '. Join (Liuliang_list)
Receive = Liuliang[1].split (' \ n ') [ -7].split () [5]
Send = Liuliang[1].split (' \ n ') [ -6].split () [5]
#print liuliang[1].split (' \ n ') [ -7].split () [5]
#print liuliang[1].split (' \ n ') [ -6].split () [5]
Receive_type = Re.findall ("kb| mb| Gb ", receive) [0]
Send_type = Re.findall ("kb| mb| Gb ", send) [0]
#print Re.findall (R ' \d+ ', receive)
#print Re.findall (R ' \d+ ', send)
Receive_num = Re.findall (R ' \d+ ', receive) [0]
Send_num = Re.findall (R ' \d+ ', send) [0]
if Receive_type = = "MB" and Send_type = = "MB":
if int (receive_num) >= and int (send_num) >= 40:
With open ('/opt/scripts/iftop.log ', ' a ') as F:
RESULT1 = "%s%s"% (Datetime.datetime.now (), header)
Time_now = str (Datetime.datetime.now ())
F.write (' \ n ' + ' ========================================================= ' + ' \ n ' + time_now + ' \ n ' + liuliang[1])
if Receive_type = = "GB" and Send_type = = "GB":
With open ('/opt/scripts/iftop.log ', ' a ') as F:
RESULT1 = "%s%s"% (Datetime.datetime.now (), header)
Time_now = str (Datetime.datetime.now ())
F.write (' \ n ' + ' ========================================================= ' + ' \ n ' + time_now + ' \ n ' + liuliang[1])
Linux server memory CPU traffic TCP exception information record Python script