diff --git a/System_Monitoring_Script/checkfile.py b/System_Monitoring_Script/checkfile.py new file mode 100644 index 0000000..86f1296 --- /dev/null +++ b/System_Monitoring_Script/checkfile.py @@ -0,0 +1,62 @@ +# !/usr/bin/python3 +# -*-coding: utf-8-*- +# by QianFeng.newrain +# +''' + Welcome QianFeng cloud computing + use mail + yag().send(to='收件人', + subject='邮件主题', + contents='邮件内容', + attachments='附件') +''' + +import yagmail +import hashlib +import os + + +def yag(): + maild = yagmail.SMTP( + user='', + password='', + host='smtp.163.com', + port='465', + smtp_ssl=True) + return maild.send + + +def md5sum(filelist, mode='r'): + hash_list = [] + for hash in filelist: + f = open(hash, mode='r') + d = hashlib.md5() + for buf in f.read(): + d.update(buf.encode()) + hash_list.append({hash: d.hexdigest()}) + if mode == 'r': + return hash_list + else: + f = open('./hash', 'w') + f.write(repr(hash_list)) + f.close() + +if __name__ == '__main__': + file = ['/Users/mingwang/Desktop/mail.py', + '/Users/mingwang/Desktop/book.py'] + hash_sum = md5sum(file) + + if os.path.isfile('./hash'): + f = open('./hash', 'r') + old_list = eval(f.read()) + n = len(old_list) - 1 + warning = [] + while n > -1: + if old_list[n] != hash_sum[n]: + warning.append(old_list[n]) + n -= 1 + print(warning) + yag()(to=[''], + subject='重要数据篡改', + contents=str(warning) + ) \ No newline at end of file diff --git a/System_Monitoring_Script/cpu.py b/System_Monitoring_Script/cpu.py new file mode 100644 index 0000000..4956378 --- /dev/null +++ b/System_Monitoring_Script/cpu.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 +#-*-coding: utf-8-*- +# 获取cpu负载信息并调用钉钉接口发送 +import subprocess,sys,datetime +import dingding +def cpu_load(): # 定义函数,获取系统cpu负载 + a = subprocess.getoutput('uptime |cut -d":" -f4') + upload = a.split() # 将结果转换为列表 + return upload # 返回 这里要注意return和print的区别 +a = cpu_load() # 调用方法 +time = datetime.datetime.now() # 定义当前时间 +data = 'cpu 负载为:{}\n当前时间:{}'.format(a,time) +dingding.msg(data) # 调用钉钉 +if __name__ == '__main__': + print(data) # 测试数据 + diff --git a/System_Monitoring_Script/os_monitor.py b/System_Monitoring_Script/os_monitor.py new file mode 100644 index 0000000..8dbe3b9 --- /dev/null +++ b/System_Monitoring_Script/os_monitor.py @@ -0,0 +1,308 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +import pexpect +import re + +import time +import threading + +''' +思路: 使用pexpect方法连接linux服务器,在远程执行系统命令获取数据,首先定义一个主方法,是连接系统,执行命令\ +然后定义各种资源方法 cpu 内存等,将命令传给主方法,处理返回数据,最后使用多进程执行各种资源方法。 +''' + +""" +主方法 +127.0.0.1#远程服务器ip地址 +""" +remoteUser = 'root' +remoteHost = '127.0.0.1' +remotePass = '1' + + +def ssh_command(user, host, password, command): + ssh_new_key = 'Are you sure you want to continue connecting' + child = pexpect.spawn('ssh -l %s %s %s' % (user, host, command)) + i = child.expect([pexpect.TIMEOUT, ssh_new_key, 'password: ']) + if i == 0: + print 'ERROR!' + print 'SSH could not login. Here is what SSH said:' + print child.before, child.after + return None + if i == 1: + child.sendline('yes') + child.expect('password: ') + i = child.expect([pexpect.TIMEOUT, 'password: ']) + if i == 0: + print 'ERROR!' + print 'SSH could not login. Here is what SSH said:' + print child.before, child.after + return None + child.sendline(password) + return child + + +""" +内存监控 +""" +def mem_info(): + + child = ssh_command(remoteUser, remoteHost, remotePass, "cat /proc/meminfo") + child.expect(pexpect.EOF) + mem = child.before + mem_values = re.findall("(\d+)\ kB", mem) + MemTotal = mem_values[0] + MemFree = mem_values[1] + Buffers = mem_values[2] + Cached = mem_values[3] + SwapCached=mem_values[4] + SwapTotal = mem_values[13] + SwapFree = mem_values[14] + print '******************************内存监控*********************************' + print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" + print "总内存:",MemTotal + print "空闲内存:", MemFree + print "给文件的缓冲大小:",Buffers + print "高速缓冲存储器使用的大小:", Cached + print "被高速缓冲存储用的交换空间大小:", SwapCached + print "给文件的缓冲大小:", Buffers + if int(SwapTotal) == 0: + print u"交换内存总共为:0" + else: + Rate_Swap = 100 - 100*int(SwapFree)/float(SwapTotal) + print u"交换内存利用率:", Rate_Swap + Free_Mem = int(MemFree) + int(Buffers) + int(Cached) + Used_Mem = int(MemTotal) - Free_Mem + Rate_Mem = 100*Used_Mem/float(MemTotal) + print u"内存利用率:", str("%.2f" % Rate_Mem), "%" + + +""" +内核线程、虚拟内存、磁盘、陷阱和 CPU 活动的统计信息 +""" +def vm_stat_info(): + child = ssh_command(remoteUser, remoteHost, remotePass, "vmstat 1 2 | tail -n 1") + child.expect(pexpect.EOF) + vmstat_info = child.before.strip().split() + processes_waiting = vmstat_info[0] + processes_sleep = vmstat_info[1] + swpd = vmstat_info[2] + free = vmstat_info[3] + buff = vmstat_info[4] + cache = vmstat_info[5] + si = vmstat_info[6] + so = vmstat_info[7] + io_bi = vmstat_info[8] + io_bo = vmstat_info[9] + system_interrupt = vmstat_info[10] + system_context_switch = vmstat_info[11] + cpu_user = vmstat_info[12] + cpu_sys = vmstat_info[13] + cpu_idle = vmstat_info[14] + cpu_wait = vmstat_info[15] + st=vmstat_info[16] + print '****************************内核线程、虚拟内存、磁盘、陷阱和 CPU 活动的统计信息监控****************************' + print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" + print "等待运行进程的数量:", processes_waiting + print "处于不间断状态的进程:", processes_sleep + print "使用虚拟内存(swap)的总量:", swpd + print "空闲的内存总量:", free + print "用作缓冲的内存总量:", buff + print "用作缓存的内存总量:", cache + print "交换出内存总量 :", si + print "交换入内存总量 :", so + print "从一个块设备接收:", io_bi + print "发送到块设备:", io_bo + print "每秒的中断数:", system_interrupt + print "每秒的上下文切换数:", system_context_switch + print "用户空间上进程运行的时间百分比:", cpu_user + print "内核空间上进程运行的时间百分比:", cpu_sys + print "闲置时间百分比:", cpu_idle + print "等待IO的时间百分比:", cpu_wait + print "从虚拟机偷取的时间百分比:", st + + +''' +cpu监控 +''' +def cpu_info(): + child = ssh_command(remoteUser, remoteHost, remotePass, "cat /proc/cpuinfo") + child.expect(pexpect.EOF) + cpuinfo = child.before + cpu_num = re.findall('processor.*?(\d+)', cpuinfo)[-1] + cpu_num = str(int(cpu_num) + 1) + print '***************************************cpu监控***************************************' + print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" + print u"CPU数目:", cpu_num + li = cpuinfo.replace('\t', '').split('\r') + CPUinfo = {} + procinfo = {} + nprocs = 0 + for line in li: + if line.find("processor") > -1: + CPUinfo['CPU%s' % nprocs] = procinfo + nprocs = nprocs + 1 + else: + if len(line.split(':')) == 2: + procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip() + else: + procinfo[line.split(':')[0].strip()] = '' + for processor in CPUinfo.keys(): + print "CPU属于的名字及其编号、标称主频:",CPUinfo[processor]['model name'] + print "CPU属于其系列中的哪一代的代号:", CPUinfo[processor]['model'] + print "CPU制造商:", CPUinfo[processor]['vendor_id'] + print "CPU产品系列代号:", CPUinfo[processor]['cpu family'] + print "CPU的实际使用主频:", CPUinfo[processor]['cpu MHz'] + + +""" +负载均衡 +""" +def load_stat(): + child = ssh_command(remoteUser, remoteHost, remotePass, "cat /proc/loadavg") + child.expect(pexpect.EOF) + loadavgs = child.before.strip().split() + print '************************负载均衡监控****************************' + print "*******************时间:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"******************" + print "系统5分钟前的平均负载:", loadavgs[0] + print "系统10分钟前的平均负载:", loadavgs[1] + print "系统15分钟前的平均负载:", loadavgs[2] + print "分子是正在运行的进程数,分母为总进程数:",loadavgs[3] + print "最近运行的进程id:", loadavgs[4] + + + +""" +获取网络接口的输入和输出 +""" +def ionetwork(): + child = ssh_command(remoteUser, remoteHost, remotePass, "cat /proc/net/dev") + child.expect(pexpect.EOF) + netdata = child.before + li = netdata.strip().split('\n') + print '************************获取网络接口的输入和输出监控****************************' + print "*******************时间:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"******************" + net = {} + for line in li[2:]: + line = line.split(":") + eth_name = line[0].strip() + # if eth_name != 'lo': + net_io = {} + net_io['Receive'] = round(float(line[1].split()[0]) / (1024.0 * 1024.0), 2) + net_io['Transmit'] = round(float(line[1].split()[8]) / (1024.0 * 1024.0), 2) + net[eth_name] = net_io + print net + + +""" +磁盘空间监控 +""" +def disk_stat(): + child = ssh_command(remoteUser, remoteHost, remotePass, "df -h") + child.expect(pexpect.EOF) + disk = child.before + disklist = disk.strip().split('\n') + disklists=[] + for disk in disklist: + disklists.append(disk.strip().split()) + print '************************磁盘空间监控****************************' + print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" + for i in disklists[1:]: + print "\t文件系统:", i[0], + print "\t容量:", i[1], + print "\t已用:", i[2], + print "\t可用:", i[3], + print "\t已用%挂载点:", i[4] + + +""" +端口监控 +一般是远程服务器用户名用户 +""" +def getComStr(): + child = ssh_command(remoteUser, remoteHost, remotePass, "netstat -tpln") + child.expect(pexpect.EOF) + Com = child.before + print '******************************端口监控*********************************' + print "*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************" + print Com + + + +""" +获取网络接口的输入和输出 +""" +def cpu(): + child = ssh_command(remoteUser, remoteHost, remotePass, 'cat /proc/stat | grep "cpu "') + child.expect(pexpect.EOF) + child1 = ssh_command(remoteUser, remoteHost, remotePass, 'cat /proc/stat | grep "cpu "') + child1.expect(pexpect.EOF) + cpus = child.before.strip().split() + cpus1 = child1.before.strip().split() + print '************************cpu使用情况****************************' + print "*******************时间:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"******************" + T1=int(cpus[1])+int(cpus[2])+int(cpus[3])+int(cpus[4])+int(cpus[5])+int(cpus[6])+int(cpus[8])+int(cpus[9]) + T2=int(cpus1[1]) + int(cpus1[2]) + int(cpus1[3]) + int(cpus1[4] )+ int(cpus1[5] )+int( cpus1[6] )+ int(cpus1[8] )+ int(cpus1[9]) + Tol=T2-T1 + Idle=int(cpus1[4]) - int(cpus[4]) + print '总的cpu时间1:',T1 + print '总的cpu时间2:', T2 + print '时间间隔内的所有时间片:', Tol + print '计算空闲时间idle:', Idle + print "计算cpu使用率:",100*(Tol-Idle)/Tol,"%" + +""" +第一种执行 +""" +def alltask(): + try: + threads = [] + t1 = threading.Thread(target=mem_info) + threads.append(t1) + t2 = threading.Thread(target=vm_stat_info) + threads.append(t2) + t3 = threading.Thread(target=cpu_info) + threads.append(t3) + t4 = threading.Thread(target=load_stat) + threads.append(t4) + t5 = threading.Thread(target=ionetwork) + threads.append(t5) + t6 = threading.Thread(target=disk_stat) + threads.append(t6) + t7 = threading.Thread(target=getComStr) + threads.append(t7) + t8 = threading.Thread(target=cpu) + threads.append(t8) + for n in range(len(threads)): + threads[n].start() + except Exception, e: + print str(e) + +""" +第二种执行 +""" +if __name__ == '__main__': + try: + threads = [] + t1 = threading.Thread(target=mem_info) + threads.append(t1) + t2 = threading.Thread(target=vm_stat_info) + threads.append(t2) + t3 = threading.Thread(target=cpu_info) + threads.append(t3) + t4 = threading.Thread(target=load_stat) + threads.append(t4) + t5 = threading.Thread(target=ionetwork) + threads.append(t5) + t6 = threading.Thread(target=disk_stat) + threads.append(t6) + t7 = threading.Thread(target=getComStr) + threads.append(t7) + t8 = threading.Thread(target=cpu) + threads.append(t8) + for n in range(len(threads)): + threads[n].start() + except Exception, e: + print str(e) \ No newline at end of file diff --git a/System_Monitoring_Script/paramiko_test.py b/System_Monitoring_Script/paramiko_test.py new file mode 100644 index 0000000..74dd4d0 --- /dev/null +++ b/System_Monitoring_Script/paramiko_test.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 +# _*_coding: utf-8_*_ + +import paramiko + + +class OP_test(): + def __init__(self, hostname, + username='root', + password=None, + port=22): + self.username = username + self.hostname = hostname + self.password = password + self.port = port + self.Tran = paramiko.Transport((self.hostname, self.port)) + self.Tran.connect(username=self.username, password=self.password) + + def ssh(self, cmd): + try: + self.ssh_obj = paramiko.SSHClient() + self.ssh_obj._transport = self.Tran + __stdin, stdout, stderr = self.ssh_obj.exec_command(cmd) + if stdout.read(): + return str(stdout.read(), encoding='utf-8') + else: + return str(stderr.read(), encoding='utf-8') + except Exception as e: + print(e) + finally: + self.Tran.close() + + def sftp(self, src, dst, mode='get'): + try: + self.sftp_obj = paramiko.SFTPClient.from_transport(self.Tran) + if mode == 'get': + self.sftp_obj.get(dst, src) + return True + elif mode == 'put': + self.sftp_obj.put(src, dst) + return True + except Exception as e: + print(e) + finally: + self.Tran.close() + + +ssh_test = OP_test(hostname='10.20.154.108', username='root', password='123123', port=22) +print(ssh_test.ssh('ls /tt')) +#result = ssh_test.sftp('/Users/mingwang/Desktop/test.py','/tmp/test.py.77',mode='put') +#result = ssh_test.sftp('/etc','Users/mingwang/Desktop/test',mode='get') diff --git a/System_Monitoring_Script/ssh.py b/System_Monitoring_Script/ssh.py new file mode 100644 index 0000000..6c81f3f --- /dev/null +++ b/System_Monitoring_Script/ssh.py @@ -0,0 +1,34 @@ +import paramiko + +''' +思路:使用paramiko工具连接服务器,并将执行命令的结果返回给程序,这里我们使用了sshclient方法\ +首先定义类,构造函数中传入了需要用到的属性 账号信息,定义op方法实现执行命令功能。 +''' +class sshd(): + def __init__(self,hostname,passwd,port=22,username='root'): # 定义函数传入连接远程用到的属性 + self.ssh = paramiko.SSHClient() # 调用sshclient方法 + self.hostname=hostname # 转换属性 + self.port = port + self.username = username + self.passwd = passwd + def op(self,cmd): + self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接不在know_hosts里的主机 + self.ssh.connect(hostname=self.hostname, + port=self.port, + username=self.username, + password=self.passwd) + stdin, stdout, stderr = self.ssh.exec_command(cmd) + self.stdin = stdin + self.stdout= str(stdout.read(),encoding='utf-8') #将结果返回并解码 + self.stderr= str(stderr.read(),encoding='utf-8') + self.ssh.close() + if self.stdout: + return self.stdout + else: + return self.stderr + def __str__(self): + return 'QianFeng cloud computing testing' +if __name__ == '__main__': + aa = sshd(hostname='172.16.147.151',passwd='1') + s = aa.op('ls /root') + print(s,end="")