{"id":4342,"date":"2021-07-11T18:15:42","date_gmt":"2021-07-11T10:15:42","guid":{"rendered":"http:\/\/blog.ywdevops.cn\/?p=4342"},"modified":"2022-06-07T17:39:24","modified_gmt":"2022-06-07T09:39:24","slug":"plugin-2","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2021\/07\/11\/plugin-2\/","title":{"rendered":"\u591c\u83ba\u76d1\u63a7\u63d2\u4ef6(v3)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>1\u3001\u5224\u65adhttps\u8bc1\u4e66\u662f\u5426\u8fc7\u671f\u7684\u63d2\u4ef6\uff0c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n: '\n   \u6b64\u811a\u672c\u7528\u6765\u83b7\u53d6https\u8bc1\u4e66\u8fc7\u671f\u65f6\u95f4\n'\n#\u8bc1\u4e66cer\u6587\u4ef6\u8def\u5f84\nCER_FILE=\/usr\/local\/n9e\/plugin\/cert\/blog.ywdevops.cn.cer\nip=$(ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1)\n#\u83b7\u53d6\u8bc1\u4e66\u5230\u671f\u65f6\u95f4\nlast_time () {\n#\u4f7f\u7528openssl\u547d\u4ee4\u6765\u83b7\u53d6\u8bc1\u4e66\u7684\u5230\u671f\u65f6\u95f4  \n   END_TIME=$(echo | openssl x509 -in ${CER_FILE} -noout -text | grep After | awk '{print $4,$5,$7}')\n\n   END_TIME1=$(date +%s -d \"$END_TIME\")\n\n   NOW_TIME=$(date +%s -d \"$(date \"+%Y-%m-%d %H:%M:%S\")\")\n#\u65f6\u95f4\u8f6c\u6362\n   value=$(($(($END_TIME1 - $NOW_TIME))\/(60*60*24)))\n}\nlast_time\necho '[\n  {\n    \"endpoint\": \"'${ip}'\",\n    \"timestamp\": '$(date +%s)',\n    \"tags\": \"cert=ghs.sfair.com.cer\",\n    \"metric\": \"ghs.cer\",\n    \"value\": '${value}'\n  }\n]'\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2\u3001nginx\u6307\u6807\u91c7\u96c6\u63d2\u4ef6(\u9700\u8981\u63d0\u524d\u5b89\u88c5vts\u6a21\u5757)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python\n#-*- coding:utf-8 -*-  #\u6307\u5b9a\u7f16\u7801\n'''\n @\u6b64\u811a\u672c\u7528\u6765\u83b7\u53d6nginx\u7684\u76d1\u63a7\u72b6\u6001\u4fe1\u606f\uff0cjson\u754c\u9762\u7684\u5404\u4e2a\u53c2\u6570\u8bf4\u660e\u5982\u4e0b:\n   servermain:\n      hostname: \u4e3b\u673a\u540d\n      nginxVersion: nginx\u7248\u672c\n      loadMsec: \u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u7684\u5904\u7406\u65f6\u95f4\n      nowMsec: \u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u7684\u5f53\u524d\u65f6\u95f4\n      connections:\n          active:     \u5f53\u524d\u6d3b\u52a8\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u6570\n          reading:    \u8bfb\u53d6\u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u603b\u6570\n          writing:    \u5199\u5165\u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u603b\u6570\n          waiting:    \u6b63\u5728\u7b49\u5f85\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u603b\u6570\n          accepted:   \u63a5\u53d7\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u603b\u6570\n          handled:    \u5df2\u5904\u7406\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u603b\u6570\n          requests:   \u8bf7\u6c42\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u603b\u6570\n          sharedZones:\n          name:       \u914d\u7f6e\u4e2d\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\u540d\u79f0\n          maxSize:    \u914d\u7f6e\u4e2d\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\u7684\u6700\u5927\u5927\u5c0f\u7684\u9650\u5236\n          usedSize:   \u5171\u4eab\u5185\u5b58\u7684\u5f53\u524d\u5927\u5c0f\n          usedNode:   \u5171\u4eab\u5185\u5b58\u4e2d\u5f53\u524d\u4f7f\u7528\u7684\u8282\u70b9\u6570\n   serverZones:\n      requestCounter: \u4ece\u5ba2\u6237\u7aef\u63a5\u6536\u7684\u5ba2\u6237\u7aef\u8bf7\u6c42\u603b\u6570\n      inBytes:    \u4ece\u5ba2\u6237\u7aef\u63a5\u6536\u7684\u603b\u5b57\u8282\u6570\n      outBytes:   \u53d1\u9001\u5230\u5ba2\u6237\u7aef\u7684\u603b\u5b57\u8282\u6570\n      responses: \n          1xx,2xx,3xx,4xx,5xx: \u8868\u793a\u54cd\u5e94\u72b6\u6001\u5417\n          miss:       \u672a\u547d\u4e2d\u7684\u7f13\u5b58\u6570\n          bypass:     \u7ed5\u8fc7\u7f13\u5b58\u65c1\u8def\u6570\n          expired:    \u8fc7\u671f\u7684\u7f13\u5b58\u6570\n          stale:      \u751f\u6548\u7f13\u5b58\u7684\u6570\u91cf\n          updating:   \u7f13\u5b58\u66f4\u65b0\u7684\u6b21\u6570\n          revalidated:\u91cd\u65b0\u9a8c\u8bc1\u7684\u7f13\u5b58\u6570\n          hit:        \u7f13\u5b58\u547d\u4e2d\u6570\n          scarce:     \u672a\u8fbe\u7f13\u5b58\u8981\u6c42\u7684\u8bf7\u6c42\u6b21\u6570\n      requestMsecCounter: \u7d2f\u79ef\u7684\u8bf7\u6c42\u5904\u7406\u65f6\u95f4(\u6beb\u79d2)\n      requestMsec:   \u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u7684\u5e73\u5747\u503c(\u6beb\u79d2)\n   upstreamZones:\n       server: server\u5730\u5740\n       requestCounter: \u8f6c\u53d1\u5230\u6b64\u670d\u52a1\u5668\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u603b\u6570\n       inBytes: \u4ece\u8be5\u670d\u52a1\u5668\u63a5\u6536\u7684\u603b\u5b57\u8282\u6570\n       outBytes:\u53d1\u9001\u5230\u6b64\u670d\u52a1\u5668\u7684\u603b\u5b57\u8282\u6570\n       responses:\n           1xx,2xx,3xx,4xx,5xx: \u8868\u793a\u54cd\u5e94\u72b6\u6001\u5417\n   requestMsecCounter: \u5305\u62ecupstream\u5728\u5185\u7684\u7d2f\u8ba1\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u6570(\u6beb\u79d2)\n   requestMsec:\n       time: \u8bf7\u6c42\u5904\u7406\u65f6\u95f4\n       msecs: \u5305\u62ecupstream\u5728\u5185\u7684\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\n   responseMsecCounter: \u4ec5upstream\u54cd\u5e94\u5904\u7406\u65f6\u95f4\u7d2f\u52a0\u503c(\u6beb\u79d2)\n   responseMsecs: \u4ec5upstream\u54cd\u5e94\u5904\u7406\u65f6\u95f4\u5e73\u5747\u503c(\u6beb\u79d2) \n   weight:      server\u7684\u5f53\u524dweight\u503c\n   maxFails:    server\u7684\u5f53\u524dmax_fails\u8bbe\u7f6e\n   failTimeout: server\u7684\u5f53\u524d\u7684fail_timeout\u8bbe\u7f6e\n   backup:      server\u7684\u5f53\u524dbackup\u8bbe\u7f6e\n   down:        \u670d\u52a1\u5668\u7684\u5f53\u524ddown\u8bbe\u7f6e\n   \n'''\nimport requests\nimport json\nimport time\nimport commands\nclass Check_Nginx:\n    ''' \u6b64\u6765\u7528\u6765\u68c0\u6d4bnginx\u6307\u6807'''\n    def __init__(self,result):\n        self.result = result\n        print(json.dumps(self.result)) #\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b,\u5426\u5219\u591c\u83ba\u4e0d\u8bc6\u522b\ndef main():\n    result = &#91;]\n    ip = commands.getoutput('''ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1''')\n    timestamp = int(time.time())\n    r = requests.get('http:\/\/10.88.10.35\/vhost_status\/format\/json')\n    dictt = json.loads(r.text)  #\u5c06json\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\n    '''\n    \u4e0b\u9762\u4e09\u884c\u7528\u6765\u83b7\u53d6server main\u4e2d\u7684request\u4fe1\u606f\n    nginx\u8fd0\u884c\u65f6\u95f4\u7b49\u4e8enowMsec-loadMsec\uff0c\u7ed3\u679c\u503c\u5355\u4f4d\u4e3a\u6beb\u79d2\n    '''\n    nowMsec = float(dictt.get('nowMsec')) #\u5c06\u6574\u5f62\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\n    loadMsec = float(dictt.get('loadMsec'))\n    nginx_uptime = round((nowMsec-loadMsec)\/3600000,2)      #nginx\u8fd0\u884c\u65f6\u95f4,\u5355\u4f4d\u5c0f\u65f6\n    nginx_accept = dictt.get('connections').get('accepted')\n    nginx_handled = dictt.get('connections').get('handled')\n    nginx_requests = dictt.get('connections').get('requests')\n    nginx_active = dictt.get('connections').get('active')\n    nginx_reading = dictt.get('connections').get('reading')\n    nginx_writing = dictt.get('connections').get('writing')\n    nginx_waiting = dictt.get('connections').get('waiting')\n    nginx_uptime = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.uptime',\n        'value': nginx_uptime\n    }\n    nginx_accept = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.accept',\n        'value': nginx_accept\n    }\n    nginx_handled = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.handled',\n        'value': nginx_handled\n    }\n    nginx_request = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.requests',\n        'value': nginx_requests\n    }\n    nginx_active = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.active',\n        'value': nginx_active\n    }\n    nginx_reading = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.reading',\n        'value': nginx_reading\n    }\n    nginx_writing = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.writing',\n        'value': nginx_writing\n    }\n    nginx_waiting = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.waiting',\n        'value': nginx_waiting\n    }\n    result.append(nginx_uptime)\n    result.append(nginx_accept)\n    result.append(nginx_handled)\n    result.append(nginx_request)\n    result.append(nginx_active)\n    result.append(nginx_reading)\n    result.append(nginx_writing)\n    result.append(nginx_waiting)\n    '''\n    \u4e0b\u9762\u7528\u6765\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\n    '''\n    sharememory_maxsize = dictt.get('sharedZones').get('maxSize')\n    sharememory_usedsize = dictt.get('sharedZones').get('usedSize')\n    sharememory_usedNode = dictt.get('sharedZones').get('usedNode')\n    server_maxsize = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.sharememory.maxsize',\n        'value': sharememory_maxsize\n    }\n    server_usedsize = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.sharememory.usedsize',\n        'value': sharememory_usedsize\n    }\n    server_usedNode = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.sharememory.usedNode',\n        'value': sharememory_usedNode\n    }\n    result.append(server_maxsize)\n    result.append(server_usedsize)\n    result.append(server_usedNode)\n    '''\n    \u4e0b\u9762\u7684\u7528\u6765\u83b7\u53d6server zones\u4e2d\u7684\u4fe1\u606f\n    '''\n    server_requestCounter = dictt.get('serverZones').get('localhost').get('requestCounter')#serverzone\u7684requests\n    server_inbytes = dictt.get('serverZones').get('localhost').get('inBytes') #traffic Rcvd\n    server_outbytes = dictt.get('serverZones').get('localhost').get('outBytes') #traffic send\n    server_code1 = dictt.get('serverZones').get('localhost').get('responses').get('1xx')\n    server_code2 = dictt.get('serverZones').get('localhost').get('responses').get('2xx')\n    server_code3 = dictt.get('serverZones').get('localhost').get('responses').get('3xx')\n    server_code4 = dictt.get('serverZones').get('localhost').get('responses').get('4xx')\n    server_code5 = dictt.get('serverZones').get('localhost').get('responses').get('5xx')\n    server_requestCounter = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.requestCounter',\n        'value': server_requestCounter\n    }\n    server_inbytes = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.inbytes',\n        'value': server_inbytes\n    }\n    server_outbytes = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.outbytes',\n        'value': server_outbytes\n    }\n    server_code1 = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.1xx',\n        'value': server_code1\n    }\n    server_code2 = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.2xx',\n        'value': server_code2\n    }\n    server_code3 = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.3xx',\n        'value': server_code3\n    }\n    server_code4 = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.4xx',\n        'value': server_code4\n    }\n    server_code5 = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'nginx.server.5xx',\n        'value': server_code5\n    }\n    result.append(server_requestCounter)\n    result.append(server_inbytes)\n    result.append(server_outbytes)\n    result.append(server_code1)\n    result.append(server_code2)\n    result.append(server_code3)\n    result.append(server_code4)\n    result.append(server_code5)\n    '''\u4e0b\u9762\u7684\u7528\u6765\u83b7\u53d6upstream\u7684\u4fe1\u606f'''\n    upstreamZones = dictt.get('upstreamZones').get('schedule')\n    for i in upstreamZones:  #\u904d\u5386\u5217\u8868\n         get_upstreamip = i.get('server').split(':')&#91;0]#\u83b7\u53d6\u4ee3\u7406\u7684ip\u5730\u5740\n         #get_upstreamip = i.get('server')#\u83b7\u53d6\u4ee3\u7406\u7684ip\u5730\u5740\u548c\u7aef\u53e3\n         get_requestCounter = i.get('requestCounter')\n         get_inBytes = i.get('inBytes')\n         get_outBytes = i.get('outBytes')\n         get_upstream_1xx = i.get('responses').get('1xx')\n         get_upstream_2xx = i.get('responses').get('2xx')\n         get_upstream_3xx = i.get('responses').get('3xx')\n         get_upstream_4xx = i.get('responses').get('4xx')\n         get_upstream_5xx = i.get('responses').get('5xx')\n         get_responseMsec = i.get('responseMsec')\n         get_ip = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.ip',\n             'value': None,\n         }\n         if get_upstreamip is not None:\n             get_ip&#91;'value'] = 1\n         else:\n             get_ip&#91;'value'] = 0\n         nginx_requestCounter = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.requestCounter',\n             'value': get_requestCounter\n         }\n         nginx_inBytes = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.inBytes',\n             'value': get_inBytes\n         }\n         nginx_outBytes = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.outBytes',\n             'value': get_outBytes\n         }\n         upstream_1xx = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.1xx',\n             'value': get_upstream_1xx\n         }\n         upstream_2xx = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.2xx',\n             'value': get_upstream_2xx\n         }\n         upstream_3xx = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.3xx',\n             'value': get_upstream_3xx\n         }\n         upstream_4xx = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.4xx',\n             'value': get_upstream_4xx\n         }\n         upstream_5xx = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.5xx',\n             'value': get_upstream_5xx\n         }\n         get_responsetime = {\n             'endpoint': ip,\n             'tags': 'upstreamip=%s'%get_upstreamip,\n             'timestamp': timestamp,\n             'metric': 'nginx.upstream.responsetime',\n             'value': get_responseMsec\n         } \n         result.append(get_ip)\n         result.append(nginx_requestCounter)\n         result.append(nginx_inBytes)\n         result.append(nginx_outBytes)\n         result.append(upstream_1xx)\n         result.append(upstream_2xx)\n         result.append(upstream_3xx)\n         result.append(upstream_4xx)\n         result.append(upstream_5xx)\n         result.append(get_responsetime)\n    Check_Nginx(result)\nif __name__ == '__main__':\n    main()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001oracle\u6307\u6807\u91c7\u96c6\u63d2\u4ef6\uff0c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3\n#-*- coding:utf-8 -*-\n\"\"\"\n  @\u6b64\u811a\u672c\u7684\u529f\u80fd\u4e3a\u76d1\u63a7oracle\u6307\u6807\uff0c\u76d1\u63a7oracle\u5730\u5740\u4e3a\n   192.168.3.200\n  @author:gongguan\n  @date: 20210624\n\"\"\"\nimport cx_Oracle\nimport subprocess\nimport time\nimport json\ndef get_value():\n    ip = subprocess.getoutput('''ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1''')\n    timestamp = int(time.time())\n    result = &#91;]\n    value = 0\n    db = cx_Oracle.connect('sffoc','sfhxfoc#3','192.168.3.200:1521\/foc')\n    r = db.cursor()\n    sql = \"select sysdate - (select max(insert_date) from t7107 ) from dual\"\n    r.execute(sql)\n    get_all = r.fetchall()\n    get_result = get_all&#91;0]&#91;0]\n    db.close()\n    get_value = round(get_result,2)\n    if ( get_value > 1.05 ):\n        value = 0\n    else:\n        value = 1\n    result_value = {\n        'endpoint': ip,\n        'timestamp': timestamp,\n        'tags': 'oracle_ip=192.168.3.200',\n        'metric': 'oracle_value',\n        'value': value\n    }\n    result.append(result_value)\n    print(json.dumps(result))\nif __name__ == '__main__':\n    get_value()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4\u3001\u68c0\u6d4b\u78c1\u76d8\u662f\u5426\u6302\u8f7d\u63d2\u4ef6\uff0c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3\n#-*- coding:utf-8 -*-\nimport os\nimport platform\nimport json\nimport re\nimport time\nimport requests\nimport importlib,sys\nimport subprocess\nimportlib.reload(sys)\n#sys.setdefaultencoding(\"utf-8\")\nsystype = platform.system()\nip = subprocess.getoutput(\"hostname -i\")\ntimestamp = int(time.time())\ndef do_cmd():\n    if(systype == \"Windows\"):\n        disk_list = &#91;i.strip() for i in\n                          os.popen('C:\\\\WINDOWS\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe net use').readlines()]&#91;3:]\n        value = 0\n        result = &#91;]\n        for mount_name in mount_name_list.split(','):\n            for disk_info in disk_list:\n                disk = re.findall(r\"^&#91;ok|OK].*\\w{3}\\d{1}\\.\\w{5}\\.\\w{3}\\\\\\w{3}\",disk_info)\n                if  len(disk) !=0:\n                    strr = str(disk&#91;0])          #\u6839\u636e\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7684\u503c\n                    check_ok = strr.split()&#91;0]   #\u6839\u636e\u7a7a\u683c\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15\u503c\n                    if (check_ok == \"ok\") or (check_ok == \"OK\"):\n                        value = 1\n                    else:\n                        value = 0\n\n            result_value = {\n                'endpoint': '192.168.18.93',\n                'tags': \"\",\n                'timestamp': timestamp,\n                'metric': 'disk.mount.efb',\n                'value': value,\n                'step': 5,\n                'extra': \"\",\n            }\n            result.append(result_value)\n        url = \"http:\/\/192.168.22.183:8008\/api\/transfer\/push\"\n        r = requests.post(url=url,data=json.dumps(result))\n        print(r)\n        return result\n    elif(systype == \"Linux\"):\n        value = 0\n        linux_result = &#91;]\n        result = subprocess.getoutput(\"df -h |grep '192.168.10.4'\")\n        if result == '':\n            value = 0\n        else:\n            value = 1\n        check_result = {\n            'endpoint': ip,\n            'tags': '',\n            'timestamp': timestamp,\n            'metric': 'mount104',\n            'value': value,\n        }\n        linux_result.append(check_result)\n        print(json.dumps(linux_result))\nif __name__ == \"__main__\":\n    do_cmd()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5\u3001\u5224\u65ad\u6587\u4ef6\u5939\u65f6\u95f4\u63d2\u4ef6\uff0c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3\n# -*- coding:utf-8 -*-\nimport time\nimport datetime\nimport math\nimport json\nimport subprocess #python\u6267\u884cshell\u7528\u6b64\u6a21\u5757,python2\u7528commands\u6a21\u5757\ndef check_time():\n    '''\n    @\u672c\u811a\u672c\u4e3b\u8981\u529f\u80fd\u4e3a\u83b7\u53d6\u4e24\u4e2a\u6587\u4ef6\u5939\u65f6\u95f4\u4e0e\u7cfb\u7edf\u65f6\u95f4\u505a\u5bf9\u6bd4\n     \u5982\u679c\u65f6\u95f4\u5dee\u5927\u4e8e40\u5206\u949f\uff0c\u5219\u544a\u8b66\n    @author: gongguan\n    @time:  20210529\n    '''\n    result = &#91;]\n    value = 0\n    value1 = 0\n    ip = subprocess.getoutput('''ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1''')\n    timestamp = int(time.time())\n    date_time = datetime.datetime.now() #\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4,\u6ce8\u610f\u6b64\u65f6\u7684\u7c7b\u578b\u4e3adata_time\u65e5\u671f\u7c7b\u578b\n    sys_time = str(date_time).split(' ')&#91;1] #\u5982\u679c\u8981\u7528split\u5207\u5272,\u9700\u8981\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u624d\u80fd\u5207\u5272\n    sys_hour = sys_time.split(':')&#91;0]  #\u83b7\u53d6\u7cfb\u7edf\u7684\u5c0f\u65f6\n    sys_min = sys_time.split(':')&#91;1]   #\u83b7\u53d6\u7cfb\u7edf\u7684\u5206\u949f\u6570\n    one_modify_time = subprocess.getoutput('''stat one | grep Modify | awk '{print $3}' |awk -F \".\" '{print $1}' ''')\n    one_hour = subprocess.getoutput(''' stat one | grep Modify | awk '{print $3}' |awk -F \".\" '{print $1}' |awk -F ':' '{print $1}' ''')\n    one_min=subprocess.getoutput(''' stat one | grep Modify | awk '{print $3}' |awk -F \".\" '{print $1}' |awk -F ':' '{print $2}' ''')\n    two_hour = subprocess.getoutput(''' stat one | grep Modify | awk '{print $3}' |awk -F \".\" '{print $1}' |awk -F ':' '{print $1}' ''')\n    two_min = subprocess.getoutput(''' stat one | grep Modify | awk '{print $3}' |awk -F \".\" '{print $1}' |awk -F ':' '{print $2}' ''')\n    #\u63a5\u4e0b\u6765\u5c06\u83b7\u53d6\u5230\u7684\u6587\u4ef6\u5939\u65f6\u95f4\u4e0e\u7cfb\u7edf\u7684\u65f6\u95f4\u505a\u5bf9\u6bd4\u5e76\u6267\u884c\u903b\u8f91\n    if (abs(int(sys_hour) - int(one_hour)) == 23 ) and ((60 - int(sys_min) + int(one_min)) &lt;= 40):\n        value = 1\n    elif (abs(int(sys_hour) - int(one_hour)) == 0 ) and ( abs(int(sys_min) - int(one_min)) &lt;= 40 ):\n        value = 1\n    else:\n        value = 0\n    one_time = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'one.time',\n        'value': value\n    }\n    result.append(one_time)\n    #\u63a5\u4e0b\u6765\u83b7\u53d6prog\u6587\u4ef6\u5939\u65f6\u95f4\uff0c\u4e0e\u7cfb\u7edf\u65f6\u95f4\u505a\u5bf9\u6bd4\u5e76\u6267\u884c\u903b\u8f91\n    if (abs(int(sys_hour) - int(two_hour)) == 23 ) and ((60 - int(sys_min) + int(two_min)) &lt;= 40):\n        value1 = 1\n    elif (abs(int(sys_hour) - int(two_hour)) == 0 ) and ( abs(int(sys_min) - int(two_min)) &lt;= 40 ):\n        value1 = 1\n    else:\n        value1 = 0\n    two_time = {\n        'endpoint': ip,\n        'tags': '',\n        'timestamp': timestamp,\n        'metric': 'two.time',\n        'value': value1\n    }\n    result.append(two_time)\n    check_result = json.dumps(result)\n    print(check_result)\nif __name__ == '__main__':\n    check_time()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6\u3001\u591c\u83baurl\u4e8c\u6b21\u5904\u7406\u63d2\u4ef6\uff0c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3.6\n#coding: utf-8\n\"\"\"\n   @Created on 2021-04-21\n   @\u4f5c\u7528:\u7528\u4e8e\u591c\u83baURL\u544a\u8b66\u63a5\u53e3\u4e8c\u6b21\u5904\u7406\n   @author: gongguan\n\"\"\"\nfrom flask import Flask, request, jsonify,redirect\nimport json\nimport ast\nimport pymysql\nimport sys\nimport requests\nimport urllib3\nhttp = urllib3.PoolManager()\napp = Flask(__name__)\napp.debug = True\n#app.config&#91;'JSON_AS_ASCII'] = False\n@app.route(\"\/index\/\",methods=&#91;\"GET\",\"POST\"])\ndef post_http():\n    if request.method == 'POST':\n        get_data = request.get_data()\n        get_data = json.loads(get_data)    #\u5c06\u83b7\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\n        str_data = str(get_data)\n        file = open(\"alert.json\",\"w+\",encoding='utf-8')\n        file.write(str_data)            #\u5c06\u591c\u83ba\u63a8\u9001\u8fc7\u6765\u7684\u544a\u8b66\u4fe1\u606f\u5199\u5165\u6587\u4ef6\u4e2d\n        file.close()\n        get_alert = post_alert()\n        return str_data,get_alert\n    else:\n        return \"\u53ea\u63a5\u53d7post\u8bf7\u6c42\"\ndef post_alert():\n    varr = \"100\"\n    result_bz,result_ip = select_mysql()\n    result_name = get_name()\n    result_history = get_history()\n    result_stragety = get_stragety()\n    result_url = get_url()\n    result_code,result_value = status_code()\n    if int(result_value) == 0:\n        url = \"http:\/\/127.0.0.1:8848\/nacos\/v1\/cs\/configs\"\n        file = open(\"dictfile\",\"r\",encoding='utf-8') #dictfile\u4e3a\u57fa\u7840\u6a21\u677f\n        dictt = ast.literal_eval(file.read()) #\u5c06\u8bfb\u53d6\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\n        file.close()\n        listname = ast.literal_eval(dictt.get('content')) #\u4fee\u6539dictfile\u4e2d\u7684\u5b57\u5178\u7684\u503c\n        listname&#91;0].get('labels')&#91;'alertname'] = result_name #\u4fee\u6539\u5b57\u5178\u7684\u503c\n        listname&#91;0].get('labels')&#91;'instance'] = result_ip    #\u4fee\u6539\u5b57\u5178\u7684\u503c\n        listname&#91;0].get('labels')&#91;'value'] = result_value\n        listname&#91;0].get('labels')&#91;'target'] = result_code\n        listname&#91;0].get('annotations')&#91;'summary'] = result_bz\n        listname&#91;0].get('annotations')&#91;'description'] = result_url\n        listname&#91;0].get('annotations')&#91;'recovery_value'] = 200  #\u5176\u4f59\u90fd\u662f\u53d8\u91cf\uff0c\u552f\u72ec\u8fd9\u4e2a\u4e3a\u5e38\u91cf  \n        dictt&#91;\"content\"] = json.dumps(listname)    #\u8f6c\u6362\u4e3ajson\u683c\u5f0f\u540e\u7ed9\u5b57\u5178\u8d4b\u503c\n        requests.post(url=url ,params=dictt)  #\u5c06\u4fee\u6539\u540e\u7684\u5185\u5bb9\u63a8\u9001\u7ed9nacos\n    node_file = open('\/usr\/local\/node_exporter\/key\/key.prom','r+',encoding='utf-8')\n    node_file.write('result_value')\n    node_file.write(' ')\n    node_file.write(result_value)\n    node_file.close()\n    return \"success\"\ndef public():\n    file = open(\"alert.json\",\"r\",encoding='utf-8')            #\u6253\u5f00\u6587\u4ef6\n    rfile = file.read()                   #\u8bfb\u53d6\u6587\u4ef6\n    dict_file = ast.literal_eval(rfile)    #\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\n    file.close()                           #\u5173\u95ed\u6587\u4ef6\n    return dict_file\n\ndef get_ip():\n    dict_file = public()\n    listfile = dict_file.get('detail')     #\u83b7\u53d6\u6307\u5b9a\u7684\u952e\u503c\uff0c\u7ed3\u679c\u4e3a\u5217\u8868\n    dictt =  listfile&#91;0]                   #\u83b7\u53d6\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15\u5185\u5bb9,\u5f97\u5230\u5185\u5bb9\u8fd8\u662f\u4e00\u4e2a\u5b57\u5178\n    dictt_one = dictt.get('tags').get('host')  #\u83b7\u53d6\u952e\u503c\u4e3atags\u7684\u952e\u6240\u5bf9\u5e94\u7684\u503c,\u7ed3\u679c\u4f9d\u65e7\u662f\u4e00\u4e2a\u5b57\u5178\n    #dictt_two = dictt_one.get('host')         #\u83b7\u53d6host\u952e\u6240\u5bf9\u5e94\u7684\u503c \u4e0a\u6761\u8bed\u53e5\u4e00\u8d77\u5199\u4e86\n    ip = dictt_one.split(':')&#91;0]         #\u901a\u8fc7split\u65b9\u6cd5\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u4ee5:\u4e3a\u5206\u9694\u7b26\uff0c\u5206\u5272\u540e\u6253\u5370\u7b2c\u4e00\u4e2a\u7d22\u5f15\u5b57\u7b26\n    return ip\ndef get_name():                          #\u83b7\u53d6\u7b56\u7565\u540d\u79f0\n    name = public()                      #\u8c03\u7528\u516c\u5171\u51fd\u6570public()\n    get_name = name.get('sname')\n    return get_name\ndef get_history():                       #\u83b7\u53d6\u62a5\u8b66\u8be6\u60c5id\n    history = public()\n    get_history = history.get('id')      #\u67e5\u627e\u5b57\u5178\u952eid\u5bf9\u5e94\u7684\u503c\n    return get_history\ndef get_stragety():\n    stragety = public()\n    get_stragety = stragety.get('sid')\n    return get_stragety\ndef get_url():\n    get_url = public()\n    ip_port = get_url.get('detail')&#91;0].get('tags').get('host')\n    with open(\"\/home\/n9e\/etc\/probe.yml\",'r',encoding='utf-8') as f:\n        for line in f:\n            if line.find(ip_port) != -1:\n                add_url = line.split('-')&#91;1]\n                return add_url\ndef status_code():\n    get_code = public()\n    status_code = get_code.get('value').split(':')&#91;0]  #\u8fd4\u56de\u6307\u6807\u540d\u79f0\n    status_value = get_code.get('value').split(':')&#91;1] #\u8fd4\u56de\u5f53\u524d\u8fd4\u56de\u503c\n    return status_code,status_value\ndef select_mysql():\n    getip = get_ip()\n    conn = pymysql.connect(host=\"10.88.22.188\",user=\"root\",password=\"sf@PWD6950\",database=\"n9e_rdb\",charset='utf8')\n    cursor = conn.cursor()\n    sql = \"select note from resource where ident=%s\"\n    cursor.execute(sql,&#91;getip])\n    data = cursor.fetchone()\n    if data is None:\n        result_data = \"\u6ca1\u6709\u6570\u636e\"\n    else:\n        bz = ''.join(data)\n        return bz,getip\n    cursor.close()\n    conn.close()\nif __name__ == '__main__':\n    app.run(host='0.0.0.0',port=5001)\n    #\u8fd9\u91cc\u6307\u5b9a\u4e86\u5730\u5740\u548c\u7aef\u53e3\n#dictfile\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a\n{\n        \"dataId\": \"etc.prometheus\",\n        \"group\": \"DEFAULT_GROUP\",\n        \"content\": \"\"\"&#91;{  \\\n        \"labels\": {  \\\n            \"alertname\": \"workd\",   \\\n            \"instance\": \"910\",      \\\n            \"value\": \"1\",           \\\n            \"target\": \"code\" \\\n         }, \\\n        \"annotations\": { \\\n            \"summary\": \"15.88.22.200\" ,    \\\n            \"description\": \"\u7cfb\u7edf\u5df2\u7ecf\u6302\u4e86\uff0c\u8bf7\u68c0\u67e5\uff01\uff01\", \\\n            \"recovery_value\": \"200\" \\\n         }\n      }]\"\"\"\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5224\u65adhttps\u8bc1\u4e66\u662f\u5426\u8fc7\u671f\u7684\u63d2\u4ef6\uff0c\u5982\u4e0b\uff1a 2\u3001nginx\u6307\u6807\u91c7\u96c6\u63d2\u4ef6(\u9700\u8981\u63d0\u524d\u5b89\u88c5vts\u6a21\u5757) 3\u3001ora&#8230;<\/p>\n","protected":false},"author":3,"featured_media":4345,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109,29],"tags":[],"class_list":["post-4342","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-didi","category-yunwei"],"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/4342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4342"}],"version-history":[{"count":11,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/4342\/revisions"}],"predecessor-version":[{"id":8877,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/4342\/revisions\/8877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/4345"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}