#!/usr/bin/python
# -*- coding: UTF-8 -*-
#####################################
## ? ?created by basededato ? ? ? ?##
## ? ?2018-01-02 ? ? ? ? ? ? ? ? ? ##
## ? ?監(jiān)控
#####################################
import os
import json
class Monitor(object):
? ? def __init__(self , Cmd):
? ? ? ? self.Cmd = Cmd
? ? # vmstat cpu監(jiān)控
? ? def cpuVmstat(self):
? ? ? ? fn_data = os.popen(self.Cmd).read().strip()
? ? ? ? lines = fn_data.split('\n')
? ? ? ? us = 0
? ? ? ? sy = 0
? ? ? ? wa = 0
? ? ? ? idle = 0
? ? ? ? for i in lines[3:]: ?# 從第4行循環(huán)取數(shù)據(jù)
? ? ? ? ? ? line = i.split()
? ? ? ? ? ? us = us + int(line[-5])
? ? ? ? ? ? sy = sy + int(line[-4])
? ? ? ? ? ? wa = wa + int(line[-2])
? ? ? ? ? ? idle = idle + int(line[-3])
? ? ? ? us = us / len(lines[3:])
? ? ? ? sy = sy / len(lines[3:])
? ? ? ? wa = wa / len(lines[3:])
? ? ? ? idle = idle / len(lines[3:])
? ? ? ? # used=100-wa-idle
? ? ? ? cpuused_info = {'CPU_WA': wa, 'CPU_IDLE': idle, 'CPU_US': us, 'CPU_SY': sy}
? ? ? ? return cpuused_info
? ? # cpu_load_average監(jiān)控
? ? def cpuLoadAvreage(self):
? ? ? ? fn_data = os.popen(self.Cmd).read().strip()
? ? ? ? lines = fn_data.split('\n')
? ? ? ? for i in lines[0:]:
? ? ? ? ? ? line = i.split()
? ? ? ? ? ? load1mins = line[-3].replace(',', '')
? ? ? ? ? ? load5mins = line[-2].replace(',', '')
? ? ? ? ? ? load15mins = line[-1].replace(',', '')
? ? ? ? ? ? if line[3].replace(',', '') == 'days':
? ? ? ? ? ? ? ? uptime = (line[2] + line[3] + line[4]).replace(',', '')
? ? ? ? ? ? elif line[3].replace(',', '') == 'min':
? ? ? ? ? ? ? ? uptime = (line[2] + line[3]).replace(',', '')
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? uptime = line[2].replace(',', '')
? ? ? ? ? ? cpuload_info = {'POWER_UPTIME': uptime, 'LOAD_AVERAGE_1MINS': load1mins, 'LOAD_AVERAGE_5MINS': load5mins, 'LOAD_AVERAGE_15MINS': load15mins}
? ? ? ? ? ? return cpuload_info
? ? # 磁盤空間監(jiān)控
? ? def diskFree(self):
? ? ? ? fn_data = os.popen(self.Cmd).read().strip()
? ? ? ? lines = fn_data.split('\n')
? ? ? ? for i in lines[0:]:
? ? ? ? ? ? line = i.split()
? ? ? ? ? ? if str(line) != '[]':
? ? ? ? ? ? ? ? used = line[4]
? ? ? ? ? ? ? ? list = line[5]
? ? ? ? ? ? ? ? diskused_info = {'DISKURL': list, 'DISK_USED': used}
? ? ? ? ? ? ? ? return diskused_info
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? diskused_info = {'DISKURL': 'config error,please check cfg file'}
? ? ? ? ? ? ? ? return diskused_info
? ?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
