#! /Usr/bin/env python Import inspect Import time Import urllib, urllib2 Import json Import socket Class mon: Def _ init _ (self ): Self. data = {} Def getTime (self ): Return str (int (time. time () + 8*3600) Def getHost (self ): Return socket. gethostname () Def getLoadAvg (self ): With open ('/proc/loadavg') as load_open: A = load_open.read (). split () [: 3] Return ','. join () Def getMemTotal (self ): With open ('/proc/meminfo') as mem_open: A = int (mem_open.readline (). split () [1]) Returns a/1024 Def getMemUsage (self, noBufferCache = True ): If noBufferCache: With open ('/proc/meminfo') as mem_open: T = int (mem_open.readline (). split () [1]) F = int (mem_open.readline (). split () [1]) B = int (mem_open.readline (). split () [1]) C = int (mem_open.readline (). split () [1]) Return (T-F-B-C)/1024 Else: With open ('/proc/meminfo') as mem_open: A = int (mem_open.readline (). split () [1])-int (mem_open.readline (). split () [1]) Returns a/1024 Def getMemFree (self, noBufferCache = True ): If noBufferCache: With open ('/proc/meminfo') as mem_open: T = int (mem_open.readline (). split () [1]) F = int (mem_open.readline (). split () [1]) B = int (mem_open.readline (). split () [1]) C = int (mem_open.readline (). split () [1]) Return (F + B + C)/1024. Else: With open ('/proc/meminfo') as mem_open: Mem_open.readline () A = int (mem_open.readline (). split () [1]) Returns a/1024 Def runAllGet (self ): # Automatically obtain all the getXXX methods in the mon class, and use XXX as the key, and the return value of getXXX () as the value to construct the dictionary. For fun in inspect. getmembers (self, predicate = inspect. ismethod ): If fun [0] [: 3] = 'get ': Self. data [fun [0] [3:] = fun [1] () Return self. data If _ name _ = "_ main __": While True: M = mon () Data = m. runAllGet () Print data Req = urllib2.Request ("http://bkjia.net: 8888", json. dumps (data), {'content-type': 'application/json '}) F = urllib2.urlopen (req) Response = f. read () Print response F. close () Time. sleep (60) Nohup python moniItems. py>/dev/null 2> & 1 & run |