- 脚本参数解析:

1 #!/usr/bin/env python3
 2 #首先导入模块
 3 import argparse
 4 #创建一个解析对象
 5 parser= argparse.ArgumentParser()
 6 #向该对象中添加你要关注的命令行参数和选项
 7 parser.add_argument('-v', '--version', help='disaple version')
 8 parser.add_argument('-i', '--image', help='传入图片文件')
 9 parser.add_argument('-s', '--hostname', help='主机ip')
10 
11 ## 进行解析
12 args = parser.parse_args()
13 print(args)
14 print(args.version)
15 print(args.hostname)

 

- man函数的使用:

1 #!/usr/bin/env python3
  2 # @Project  :monitor
  3 # @Time     : 2020/4/4 11:19
  4 # @Author   : 邢飞
  5 # @File     : service-notification-rebot.py.py
  6 # @Software : PyCharm
  7 
  8 from sys import exit
  9 from time import localtime, strftime
 10 import argparse
 11 import subprocess
 12 import logging
 13 import requests
 14 
 15 logging.basicConfig(level=logging.DEBUG,
 16                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
 17                     datefmt='%a, %d %b %Y %H:%M:%S',
 18                     #filename='icinga-rebot.log',
 19                     filename='/var/log/icinga-rebot.log',
 20                     filemode='w')
 21 
 22 
 41 
 42 def send_rebot_check(msg):
 43     """
 44     发送信息到微信企业机器人
 45     :param request:
 46     :return:
 47     """
 48     message = """服务器:%s 的%s 服务出现异常,紧急状态为:%s ,请及时处理 """ % (msg.HOSTDISPLAYNAME, msg.SERVICEDISPLAYNAME, msg.SERVICESTATE)
 49 
 50     headers = {"Content-Type": "application/json"}
 51     data = {
 52         "msgtype": "text",
 53         "text": {
 54             "content": message,
 55         "mentioned_list":["@all"],
 56         }
 57     }
 58     r = requests.post(
 59         url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk',
 60         headers=headers, json=data)
 61     return r
 62 
 63 
 64 def parsArgs():
 65     parser = argparse.ArgumentParser()
 66     parser.add_argument("-d", "--LONGDATETIME", type=str, required=True, help="$LONGDATETIME$")
 67     parser.add_argument("-e", "--SERVICENAME", type=str, required=True, help="$SERVICENAME$")
 68     parser.add_argument("-l", "--HOSTNAME", type=str, required=True, help="$HOSTNAME$")
 69     parser.add_argument("-n", "--HOSTDISPLAYNAME", type=str, required=True, help="$HOSTDISPLAYNAME$")
 70     parser.add_argument("-o", "--SERVICEOUTPUT", type=str, required=True, help="$SERVICEOUTPUT$")
 71     parser.add_argument("-s", "--SERVICESTATE", type=str, required=True, help="$SERVICESTATE$")
 72     parser.add_argument("-t", "--NOTIFICATIONTYPE", type=str, required=True, help="$NOTIFICATIONTYPE$")
 73     parser.add_argument("-r", "--USEREMAIL", type=str, required=True, help="$USEREMAIL$")
 74     parser.add_argument("-u", "--SERVICEDISPLAYNAME", type=str, required=True, help="$SERVICEDISPLAYNAME$")
 75     parser.add_argument("-4", "--HOSTADDRESS", type=str, help="$HOSTADDRESS$")
 76     parser.add_argument("-6", "--HOSTADDRESS6", type=str, help="$HOSTADDRESS6$")
 77     parser.add_argument("-b", "--NOTIFICATIONAUTHORNAME", type=str, help="$NOTIFICATIONAUTHORNAME$")
 78     parser.add_argument("-c", "--NOTIFICATIONCOMMENT", type=str, help="$NOTIFICATIONCOMMENT$")
 79     parser.add_argument("-v", "--notification_sendtosyslog", type=str, help="$notification_sendtosyslog$")
 80 
 81     return parser.parse_args()
 82 
 83 
 84 def main():
 85     args = parsArgs()
 86     send_rebot_check(args)
 87     logging.debug(args)
 88     logging.debug(send_rebot_check(args))
 89 #    logging.debug(args.HOSTNAME)
 90     # 服务主机
 91     logging.debug(args.HOSTDISPLAYNAME)
 92     #  服务名称·
 93     logging.debug(args.SERVICESTATE)
 94     # 服务状态
 95     logging.debug(args.SERVICEDISPLAYNAME)
 96  #   logging.debug(args.NOTIFICATIONTYPE)
 97     logging.debug(type(args))
 98     print(args)
 99     print(type(args))
100     #send_rebot_check(msg)
101 
102 
103 if __name__ == '__main__':
104     main()