发布时间:2017-12-06 22:45:08编辑:Run阅读(4829)
运维自动化,已经成为运维必不可少的一部分,下面附上自己写的监控nginx_status脚本,大神轻喷
#!/usr/bin/python # coding: utf-8 import urllib.request import socket #自动获取主机ip地址 myname = socket.getfqdn(socket.gethostname()) myaddr = socket.gethostbyname(myname) class MonitorNginx(object): def __init__(self): self.URL = 'http://' + str(myaddr) + '/nginx_status' self.ActiveCconnections = '' self.Reading = '' self.Writing = '' self.Waiting = '' self.dict_nginx = {} self.run() def run(self): response = urllib.request.urlopen(self.URL) html = response.readlines() self.ActiveCconnections = int(str(html[0]).split(':')[1].split(' ')[1]) self.Reading = int(str(html[-1]).split(':')[1].split(' ')[1]) self.Writing = int(str(html[-1]).split(':')[2].split(' ')[1]) self.Waiting = int(str(html[-1]).split(':')[3].split(' ')[1]) self.dict_nginx['ActiveCconnections'] = self.ActiveCconnections self.dict_nginx['Reading'] = self.Reading self.dict_nginx['Writing'] = self.Writing self.dict_nginx['Waiting'] = self.Waiting print(self.dict_nginx) if __name__ == '__main__': st = MonitorNginx()
应该看到的结果
{'Waiting': 0, 'ActiveCconnections': 41, 'Writing': 41, 'Reading': 0}
这里面的值就是nginx_status上面的显示结果
47605
45985
36909
34469
29080
25713
24566
19714
19245
17756
5565°
6155°
5691°
5737°
6705°
5483°
5484°
5988°
5965°
7295°