运维自动化,已经成为运维必不可少的一部分,下面附上自己写的监控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上面的显示结果