分类: 系统运维
2013-02-16 10:52:52
研究了一下zabbix批量添加web监控。写了个批量添加监控的脚本。简要说明如下
1.配置文件
脚本需要一个名为list的配置文件。格式如 192.168.32.38 fenfen2 7073 / 。分别代表: ip地址, 工程名称(用于说明监控项目),应用程序端口,请求的uri地址。
2.ip和域名转换
脚本支持对ip和域名的双重支持。会通过dns确认最终ip
3.后继功能
批量添加trigger,并配置action实现自动重启相应web程序。
#!/bin/bash #list check [ ! -f list ] && echo \'list\' not found ,check it first;exit ### evn set USERAGENT='Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)' cur_time=`date +%s` next_time=`expr $cur_time + 120` step_name="url get" #function mysql run mysql_cmd(){ if [ -z "$1" ] then echo no sql input exit fi result=`mysql zabbix --batch --skip-column-name -e"$1"` if [ "$?" = "0" ]; then echo $result else echo MySQL command failed: $1 fi } get_next_id () { table="$1" field="$2" curr_id=`mysql_cmd "SELECT nextid FROM ids WHERE table_name='${table}' AND field_name='${field}';"` next_id=`mysql_cmd "UPDATE ids SET nextid=nextid+1 WHERE nodeid=0 AND table_name='${table}' AND field_name='${field}';SELECT nextid FROM ids WHERE table_name='${table}' AND field_name='${field}';"` if [ `expr $curr_id - $next_id` -eq -1 ] ; then echo $next_id return 1 else return 0 fi } get_test_env(){ ip_addr="" case $1 in *.test.com) ip_addr=`dig +short @192.168.32.249 $1` domain_name="$1" ;; 192.168.*) ip_addr="$1" domain_name=`host $1|awk '{print $NF}'|sed 's/\.$//g'` ;; esac if [ -z $ip_addr ] ;then echo Host not found.Check domain name or ip address. exit 1 else host_id=`mysql_cmd "select hostid from interface where ip = '$ip_addr';"` fi app_id=`mysql_cmd "select applicationid from applications where hostid = $host_id;"` base_name=`echo $domain_name |awk -F\. '{print $1}'` test_name="$base_name $2" } ### script start while read fqdnip antname port uri ;do echo $fqdnip $antname start get_test_env $fqdnip $antname #httptest httptest_id=`get_next_id 'httptest' 'httptestid'` mysql_cmd "insert into httptest(httptestid, name, applicationid, nextcheck, delay, status, agent, authentication) values (${httptest_id}, '${test_name}', ${app_id}, ${next_time}, 60, 0, '${USERAGENT}', 0)" #httpstep url_line="" httpstep_id=`get_next_id 'httpstep' 'httpstepid'` mysql_cmd "insert into httpstep(httpstepid, httptestid, name, no, url, timeout, posts, required, status_codes) values (${httpstep_id}, ${httptest_id}, '${step_name}', 1, '${url_line}', 10, '', '', 200);" #download speed for scenario item_id=`get_next_id 'items' 'itemid'` mysql_cmd "INSERT INTO items VALUES (${item_id}, 9, '', '', ${host_id}, 'Download speed for scenario \\\"\$1\\\".', 'web.test.in[${test_name},,bps]', 60, 30, 90, '', '', '', 0, 0, '', 'Bps', 0, 0, NULL, '', 0, '', '', '1', '', 0, '', NULL, NULL, '', '', '', 0, 0, '', '', '', '', 0, '', 0, '', NULL, '', '', 0, '30');" httptestitem_id=`get_next_id 'httptestitem' 'httptestitemid'` mysql_cmd "INSERT INTO httptestitem (httptestid,itemid,httptestitemid,type) VALUES (${httptest_id},${item_id},${httptestitem_id},'2');" itemapp_id=`get_next_id 'items_applications' 'itemappid'` mysql_cmd "INSERT INTO items_applications (applicationid,itemid,itemappid) VALUES ( ${app_id},${item_id},${itemapp_id});" #failed step item_id=`get_next_id 'items' 'itemid'` mysql_cmd "INSERT INTO items VALUES (${item_id}, 9, '', '', ${host_id}, 'Failed step of scenario \\\"\$1\\\".', 'web.test.fail[${test_name}]', 60, 30, 90, '0', '', '0', 0, 3, '', '', 0, 0, NULL, '', 0, '', '', '1', '', 0, '', NULL, NULL, '', '', '', 0, 0, '', '', '', '', 0, '', 0, '', NULL, '', '', 0, '30');" httptestitem_id=`get_next_id 'httptestitem' 'httptestitemid'` mysql_cmd "insert into httptestitem(httptestitemid, httptestid, itemid, type) values (${httptestitem_id}, ${httptest_id}, ${item_id}, 3)" itemapp_id=`get_next_id 'items_applications' 'itemappid'` mysql_cmd "INSERT INTO items_applications (applicationid,itemid,itemappid) VALUES ( ${app_id},${item_id},${itemapp_id});" #last error item_id=`get_next_id 'items' 'itemid'` mysql_cmd "INSERT INTO items VALUES (${item_id}, 9, '', '', ${host_id}, 'Last error message of scenario \\\"\$1\\\".', 'web.test.error[${test_name}]', 60, 30, 90, '', '', '', 0, 1, '', '', 0, 0, NULL, '', 0, '', '', '1', '', 0, '', NULL, NULL, '', '', '', 0, 0, '', '', '', '', 0, '', 0, '', NULL, '', '', 0, '30');" httptestitem_id=`get_next_id 'httptestitem' 'httptestitemid'` mysql_cmd "insert into httptestitem(httptestitemid, httptestid, itemid, type) values (${httptestitem_id}, ${httptest_id}, ${item_id}, 4)" itemapp_id=`get_next_id 'items_applications' 'itemappid'` mysql_cmd "INSERT INTO items_applications (applicationid,itemid,itemappid) VALUES ( ${app_id},${item_id},${itemapp_id});" #download speed for step item_id=`get_next_id 'items' 'itemid'` mysql_cmd "INSERT INTO items VALUES (${item_id}, 9, '', '', ${host_id}, 'Download speed for step \\\"\$2\\\" of scenario \\\"\$1\\\".', 'web.test.in[${test_name},${step_name},bps]', 60, 30, 90, '', '', '', 0, 0, '', 'Bps', 0, 0, NULL, '', 0, '', '', '1', '', 0, '', NULL, NULL, '', '', '', 0, 0, '', '', '', '', 0,'', 0, '', NULL, '', '', 0, '30');" httpstepitem_id=`get_next_id 'httptestitem' 'httptestitemid'` mysql_cmd "INSERT INTO httpstepitem (httpstepid,itemid,httpstepitemid,type) VALUES (${httpstep_id},${item_id},${httpstepitem_id},'2');" itemapp_id=`get_next_id 'items_applications' 'itemappid'` mysql_cmd "INSERT INTO items_applications (applicationid,itemid,itemappid) VALUES ( ${app_id},${item_id},${itemapp_id});" #response time item_id=`get_next_id 'items' 'itemid'` mysql_cmd "INSERT INTO items VALUES (${item_id}, 9, '', '', ${host_id}, 'Response time for step \\\"\$2\\\" of scenario \\\"\$1\\\".', 'web.test.time[${test_name},${step_name},resp]', 60, 30, 90, '', '', '', 0, 0, '', 's', 0, 0, NULL, '', 0, '', '', '1', '', 0, '', NULL, NULL, '', '', '', 0, 0, '', '', '', '', 0, '', 0, '', NULL, '', '', 0, '30');" httpstepitem_id=`get_next_id 'httpstepitem' 'httpstepitemid'` mysql_cmd "insert into httpstepitem(httpstepitemid, httpstepid, itemid, type) values (${httpstepitem_id}, ${httpstep_id}, ${item_id}, 1)" itemapp_id=`get_next_id 'items_applications' 'itemappid'` mysql_cmd "INSERT INTO items_applications (applicationid,itemid,itemappid) VALUES ( ${app_id},${item_id},${itemapp_id});" #response code item_id=`get_next_id 'items' 'itemid'` mysql_cmd "INSERT INTO items VALUES (${item_id}, 9, '', '', ${host_id}, 'Response code for step \\\"\$2\\\" of scenario \\\"\$1\\\".', 'web.test.rspcode[${test_name},${step_name}]', 60, 30, 90, '', '', '', 0, 3, '', '', 0, 0, NULL, '', 0, '', '', '1', '', 0, '', NULL, NULL, '', '', '', 0, 0, '', '', '', '', 0, '', 0, '', NULL, '', '', 0, '30');" httpstepitem_id=`get_next_id 'httpstepitem' 'httpstepitemid'` mysql_cmd "insert into httpstepitem(httpstepitemid, httpstepid, itemid, type) values (${httpstepitem_id}, ${httpstep_id}, ${item_id}, 0);" itemapp_id=`get_next_id 'items_applications' 'itemappid'` mysql_cmd "INSERT INTO items_applications (applicationid,itemid,itemappid) VALUES ( ${app_id},${item_id},${itemapp_id});" echo $fqdnip $antname finish done