Giám sát máy chủ cơ bản trong Python.
| #!/usr/bin/env Python
|
| ################# sau ################## sau
|
| # Tệp: server_monitor.py
|
| # Tên: Màn hình máy chủ
|
| # Mô tả: Thực hiện HealthCheck trên máy chủ và thông báo qua e-mail nếu phát hành ocurrs.
|
| # Phiên bản: 0.1
|
| ################# sau ################## sau
|
|
|
| # Tệp: server_monitor.py
|
| # Tên: Màn hình máy chủ os import system
|
| fromurllib2importurlopen urllib2 import urlopen
|
| # Mô tả: Thực hiện HealthCheck trên máy chủ và thông báo qua e-mail nếu phát hành ocurrs. socket import socket
|
| # Phiên bản: 0.1 sys import argv
|
| # Tải các mô -đun cho các kết nối HTTP (S) và TCP và đối số I/O. time import asctime
|
|
|
| Fromosimportsystem
|
| từSocketImportSocket usage():
|
| từSysimportArgv('%s \n' % (argv[0]))
|
| FromTimeImportAsctime('\ttest-type \ttcp or http')
|
| # Danh sách trợ giúp.('\tserver-info \thostname:port for tcp')
|
| defusage ():('\t \thttp://hostname/page for http')
|
| in ('% s \ n'% (argv [0]))('\temail-address\tusername or \n')
|
|
|
| in ('\ ttest-type \ ttcp hoặc http'))
|
| in ('\ tserver-info \ thostname: cổng cho TCP') tcp_test(server_info):
|
| in ('\ t \ thttp: // hostname/page cho http') = server_info.find(':')
|
| print ('\ Temail-Address \ Tusername hoặc \ n')) cpos < 1 or cpos == len(server_info) - 1:
|
| # Thiết lập kết nối TCP với tên máy chủ: cổng và báo cáo nếu nỗ lực thành công.('You need to give server info as hostname:port.')
|
| DEFTCP_TEST (server_info):()
|
| cpos = server_info.find (':') True
|
| ifcpos:
|
| In ('Bạn cần cung cấp thông tin máy chủ dưới dạng tên máy chủ: cổng.') = socket()
|
| cách sử dụng().connect((server_info[:cpos], int(server_info[cpos+1:])))
|
| sock.close.close
|
| cpos = server_info.find (':') True
|
| ifcpos:
|
| In ('Bạn cần cung cấp thông tin máy chủ dưới dạng tên máy chủ: cổng.') False
|
|
|
| cách sử dụng()
|
| trở lại http_test(server_info):
|
| ifcpos:
|
| In ('Bạn cần cung cấp thông tin máy chủ dưới dạng tên máy chủ: cổng.') = urlopen(server_info).read()
|
| cpos = server_info.find (':') True
|
| ifcpos:
|
| In ('Bạn cần cung cấp thông tin máy chủ dưới dạng tên máy chủ: cổng.') False
|
|
|
| cách sử dụng()
|
| trở lại server_test(test_type, server_info):
|
| thử: test_type.lower() == 'tcp':
|
| Sock = Ổ cắm () tcp_test(server_info)
|
| sock.connect ((server_info [: cpos], int (server_info [cpos+1:])))) test_type.lower() == 'http':
|
| ngoại trừ: http_test(server_info)
|
| returnFalse:
|
| # Thiết lập yêu cầu HTTP (S) cho máy chủ và báo cáo nếu nỗ lực thành công.('Invalid test-type given, please use either tcp or http.')
|
| cpos = server_info.find (':') True
|
|
|
| ifcpos
|
| In ('Bạn cần cung cấp thông tin máy chủ dưới dạng tên máy chủ: cổng.') send_error(test_type, server_info, email_address):
|
| cách sử dụng() = '%s: %s %s is down' % (asctime(), test_type.upper(), server_info)
|
| trở lại = 'Server Monitor: Performed a health-check running a %s test against %s. The server is down.' % (test_type.upper(), server_info)
|
| Hệ thống ('echo "%s" | mail -s "%s"%s'%(tin nhắn, chủ đề, email_address))('echo "%s" | mail -s "%s" %s' % (message, subject, email_address))
|
|
|
| # Phương thức chính - Gọi script và các đối số phân tích.
|
| if__name __ == '__ main__': __name__ == '__main__':
|
| iflen (argv)! = 4: len(argv) != 4:
|
| in ('số sai số đối số.')('Wrong number of arguments.')
|
| cách sử dụng()()
|
| Elifnotserver_test (argv [1], argv [2]): not server_test(argv[1], argv[2]):
|
| send_error (argv [1], argv [2], argv [3])(argv[1], argv[2], argv[3])
|