{"id":11476,"date":"2023-02-24T15:34:53","date_gmt":"2023-02-24T07:34:53","guid":{"rendered":"https:\/\/www.lwops.cn\/?p=11476"},"modified":"2023-02-24T15:34:54","modified_gmt":"2023-02-24T07:34:54","slug":"zabbix%e7%9b%91%e6%8e%a7%e9%98%bf%e9%87%8c%e4%ba%91slb%e6%93%8d%e4%bd%9c%e6%8c%87%e5%bc%95","status":"publish","type":"post","link":"https:\/\/www.lwops.cn\/index.php\/interflow\/technology\/11476","title":{"rendered":"zabbix\u76d1\u63a7\u963f\u91cc\u4e91SLB\u64cd\u4f5c\u6307\u5f15"},"content":{"rendered":"\n
\u963f\u91cc\u4e91\u8d1f\u8f7d\u5747\u8861\uff08Server Load Balancer\uff0c\u7b80\u79f0SLB\uff09\u662f\u4e91\u539f\u751f\u65f6\u4ee3\u5e94\u7528\u9ad8\u53ef\u7528\u7684\u57fa\u672c\u8981\u7d20\u3002\u901a\u8fc7\u5c06\u6d41\u91cf\u5206\u53d1\u5230\u4e0d\u540c\u7684\u540e\u7aef\u670d\u52a1\u6765\u6269\u5c55\u5e94\u7528\u7cfb\u7edf\u7684\u670d\u52a1\u541e\u5410\u80fd\u529b\uff0c\u6d88\u9664\u5355\u70b9\u6545\u969c\u5e76\u63d0\u5347\u5e94\u7528\u7cfb\u7edf\u7684\u53ef\u7528\u6027\u3002 \u963f\u91cc\u4e91SLB\u5305\u542b\u9762\u54114\u5c42\u7684\u7f51\u7edc\u578b\u8d1f\u8f7d\u5747\u8861NLB\u3001\u9762\u54117\u5c42\u7684\u5e94\u7528\u578b\u8d1f\u8f7d\u5747\u8861ALB\u548c\u4f20\u7edf\u578b\u8d1f\u8f7d\u5747\u8861CLB\uff0c\u662f\u963f\u91cc\u4e91\u5b98\u65b9\u4e91\u539f\u751f\u7f51\u5173\u3002<\/p>\n\n\n\n
\u672c\u6b21\u76d1\u63a7\u65b9\u5f0f\u901a\u8fc7zabbix\u91c7\u96c6\u5668\u65b9\u5f0f\uff08\u76ee\u524d\u7f51\u4e0a\u80fd\u67e5\u5230\u7684\u6587\u7ae0\uff0c\u76d1\u63a7\u6570\u636e\u83b7\u53d6\u662f\u57fa\u4e8eagent\u7684\u3002\u7531\u4e8e\u8c03\u7528\u963f\u91cc\u4e91\u7684\u63a5\u53e3\uff0c\u8d85\u8fc7\u4e00\u5b9a\u6b21\u6570\u4f1a\u6709\u8d39\u7528\u4ea7\u751f\uff0c\u6545\u91c7\u7528\u91c7\u96c6\u5668\u65b9\u5f0f\u4f1a\u6bd4agent\u66f4\u4f18\uff09<\/p>\n\n\n\n
#!\/usr\/bin\/env python\r\n#coding=utf-8\r\n\r\nfrom aliyunsdkcore.client import AcsClient\r\nfrom aliyunsdkcore.acs_exception.exceptions import ClientException\r\nfrom aliyunsdkcore.acs_exception.exceptions import ServerException\r\nfrom aliyunsdkcore.auth.credentials import AccessKeyCredential\r\nfrom aliyunsdkcore.auth.credentials import StsTokenCredential\r\nfrom aliyunsdkslb.request.v20140515.DescribeLoadBalancersRequest import DescribeLoadBalancersRequest\r\nimport json\r\n# \u8fd9\u91cc\u586b ACCESS_KEY \u548cSECRET_KEY<\/code><\/pre>\n\n\n\ncredentials = AccessKeyCredential('********', '****')\r\n# use STS Token\r\n# credentials = StsTokenCredential('', '', '')\r\n# \u8fd9\u91cc\u586b\u8d44\u6e90\u6240\u5c5e\u533a\u57df\r\nclient = AcsClient(region_id='cn-hangzhou', credential=credentials)\r\nrequest = DescribeLoadBalancersRequest()\r\nrequest.set_accept_format('json')\r\nresponse = client.do_action_with_exception(request)\r\nr = []\r\nres = json.loads(response)\r\n\r\n# \u62fc\u63a5\u6210\u7528\u4e8ezabbix\u81ea\u52a8\u53d1\u73b0\u8d44\u6e90\u7684\u683c\u5f0f \uff0c\u8fd9\u91cc\u4f7f\u7528 \u00a0LoadBalancerId \u4f5c\u4e3a\u4e3b\u673a\u540d\u79f0 \u00a0 LoadBalancerName \u4f5c\u4e3a\u53ef\u89c1\u540d\u79f0\r\nfor i in res['LoadBalancers']['LoadBalancer']:\r\n\u00a0 \u00a0 \u00a0 \u00a0 buf = {'{#PROJECT}':'acs_slb_dashboard', '{#INSTANCE}':i['LoadBalancerId'], '{#INSTANCE_NANME}':i['LoadBalancerName']}\r\n\u00a0 \u00a0 \u00a0 \u00a0 r.append(buf)\r\nprint(json.dumps({\"data\":r}))\n<\/code><\/pre>\n\n\n\n4.\u00a0\u83b7\u53d6\u76d1\u63a7\u6570\u636e<\/strong><\/h2>\n\n\n\n#!\/usr\/bin\/env python\r\n#coding=utf-8\r\nfrom aliyunsdkcore.client import AcsClient\r\nfrom aliyunsdkcore.acs_exception.exceptions import ClientException\r\nfrom aliyunsdkcore.acs_exception.exceptions import ServerException\r\nfrom aliyunsdkcore.request import CommonRequest\r\nfrom aliyunsdkcore.auth.credentials import AccessKeyCredential\r\nfrom aliyunsdkcore.auth.credentials import StsTokenCredential\r\nfrom aliyunsdkcms.request.v20190101.DescribeMetricLastRequest import DescribeMetricLastRequest\r\nimport json,sys\r\nfrom os import popen,system\r\n# \u952e\u503c\r\nk=sys.argv[1]\r\n# \u8d44\u6e90\u6240\u5728\u533a\u57df\r\nProduce=sys.argv[2]\r\n# \u547d\u540d\u7a7a\u95f4\r\nNamespace=sys.argv[3]\r\n# ACCESS_KEY\r\nAkey=sys.argv[4]\n<\/code><\/pre>\n\n\n\n# SECRET_KEY\r\nSkey=sys.argv[5]\r\n# \u83b7\u53d6\u76d1\u63a7\u6570\u636e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6709\u6700\u5927\u503c\uff08Maximum\uff09\uff0c\u5e73\u5747\u503c\uff08avg\uff09\u7b49\r\nKdata=sys.argv[6]\r\nZABBIX_SEND_ADDR = '\/itops\/zabbix\/bin\/zabbix_sender' \u00a0 \u00a0 #zabbix_sender \u7edd\u5bf9\u8def\u5f84\r\nzabbix_server_ip = \"192.168.1.1\" \u00a0 \u00a0 \u00a0 #zabbix_server\u6216zabbix_proxy\u5730\u5740,\u7528\u4e8e\u63a5\u6536\u6570\u636e\r\nZABBIX_PORT = '10051' \u00a0 \u00a0 \u00a0#zabbix_server\u6216zabbix_proxy\u670d\u52a1\u7aef\u53e3\r\ncredentials = AccessKeyCredential(Akey, Skey)\r\n\u00a0 \u00a0 # use STS Token\r\n\u00a0 \u00a0 # credentials = StsTokenCredential('', '', '')\r\nclient = AcsClient(region_id=Produce, credential=credentials)\r\nrequest = DescribeMetricLastRequest()\r\nrequest.set_accept_format('json')\r\nrequest.set_Namespace(Namespace)\r\nrequest.set_MetricName(k)\r\nresult = client.do_action_with_exception(request)\r\n#res = response.decode()\r\nresult = json.loads(result)\r\nresult = json.loads(result['Datapoints'])\r\n#print(result)\r\n# \u901a\u8fc7zabbix_sender \u53d1\u9001\u76d1\u63a7\u6570\u636e\u5230\u76d1\u63a7\u5bf9\u8c61\r\nfor i in result:\r\n\u00a0 \u00a0 \u00a0 \u00a0 hosthost=i[\"instanceId\"]\r\n\u00a0 \u00a0 \u00a0 \u00a0 key=k\r\n\u00a0 \u00a0 \u00a0 \u00a0 value=i[Kdata]\r\n\u00a0 \u00a0 \u00a0 \u00a0 popen('%s -z %s -p %s -s %s -k %s -o %s >\/dev\/null' % (ZABBIX_SEND_ADDR,zabbix_server_ip,ZABBIX_PORT,\"'\" + hosthost + \"'\",\"'\" + key + \"'\",\"'\" + str(value) + \"'\")<\/code><\/pre>\n\n\n\n5.\u00a0\u76d1\u63a7<\/a>\u6a21\u677f\u5236\u4f5c<\/strong><\/h2>\n\n\n\n5.1 \u65b0\u589e\u81ea\u52a8\u53d1\u73b0\u89c4\u5219\u7528\u4e8e\u81ea\u52a8\u53d1\u73b0SLB\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n