-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpywmon.py
More file actions
118 lines (89 loc) · 3.39 KB
/
pywmon.py
File metadata and controls
118 lines (89 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding:utf-8 -*-
""""
INSTALL:
*/5 * * * * /path/to/python /path/to/pywmon.py &> /dev/null
"""
import os
import re
import urllib2, urllib
import time
SERVER_NAME = 'test'
API_SECRET = 'secret'
API_URL = 'http://your-id.appspot.com/receive' # or http://your-id.sinaapp.com/receive.php
SERVICES = {
'mongodb':'mongod',
'mysql':'mysqld',
'apache':'apache2',
'nginx':'nginx',
'memcache':'memcached'
}
class wmon(object):
"""docstring for wmon"""
server_name = ""
api_url = ""
api_secret = ""
partitions_exclude = '^Filesystem|tmpfs|cdrom|mnt|none|//'
services = {}
def __init__(self):
self.server_name = SERVER_NAME
self.api_url = API_URL
self.api_secret = API_SECRET
self.services = SERVICES
def checkall(self):
"""docstring for checkall"""
data = []
data.append('time:%s' % time.time())
data.append('localtime:%s' % time.strftime('%Y-%m-%d %H:%M:%S'))
data.append('cpu:%s' % self.cpu_model_name())
data.append('uptime:%s' % self.uptime())
data.append('top:%s' % self.top())
data.append('load:%s' % self.load())
data.append('partitions:%s' % self.partitions())
services = []
for s in self.services:
services.append('%s %s' % (s, self.check_service(self.services[s])))
data.append("services:%s" % '|'.join(services))
data = ";".join(data)
data = re.sub(r'[ ]+', ' ', data)
print "send:", data
return self.send(data)
def send(self, data):
req = urllib2.Request(self.api_url)
r = urllib2.urlopen(req, urllib.urlencode({'data':data, 'secret':self.api_secret, 'name':self.server_name}))
res = r.read()
r.close()
return res
def check_service(self, service):
"""docstring for check_serveice"""
result = os.popen('ps ax -o \'%%c %%P\' | awk \'{if (($2 == 1) && ($1 == "\'%s\'")) print $0}\'' % service).read().strip("\n")
result = re.sub(r'[ ]+', ' ', result)
if result:
return result
else:
return "%s 0" % service
def cpu_model_name(self):
"""docstring for cpu_model_name"""
cpuinfo = os.popen('cat /proc/cpuinfo').read()
for line in cpuinfo.split('\n'):
tmp = line.split(':', 1)
if tmp[0].strip() == 'model name':
return tmp[1].strip()
return ""
def uptime(self):
"""docstring for uptime"""
return os.popen('uptime').read().strip("\n")
def partitions(self):
"""docstring for partitions"""
return os.popen('df -h | grep -vE \'%s\' | awk \'{ print $1 " " $6 " " $2 " " $3 " " $4 " " $5 }\'' % self.partitions_exclude).read().strip("\n").replace('\n', '|')
def top(self):
"""docstring for top"""
result = os.popen('ps axeo "%C %U %c" --sort -pcpu | head -n 7 | tail -n 6').read().replace('\n', '|').strip("|").replace('| ', '|').replace(' |', '|')
result = re.sub(r'[|]+', '|', result)
result = re.sub(r'[ ]+', ' ', result)
return result.strip()
def load(self):
"""docstring for load"""
return os.popen("cat /proc/loadavg | cut -d' ' -f1,2,3 --output-delimiter=','").read().strip("\n")
if __name__ == '__main__':
print wmon().checkall()