一 應(yīng)用場(chǎng)景描述
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計(jì),劍川網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:劍川等地區(qū)。劍川做網(wǎng)站價(jià)格咨詢:18980820575
網(wǎng)站的可用性監(jiān)控對(duì)于運(yùn)維來(lái)說是一項(xiàng)重要的工作。網(wǎng)站監(jiān)控主要關(guān)心幾個(gè)要點(diǎn):域名解析時(shí)間、域名響應(yīng)時(shí)間、域名訪問響應(yīng)代碼、域名訪問速度等。這里的域名應(yīng)該是一個(gè)專門用于網(wǎng)站監(jiān)控檢測(cè)的URL。網(wǎng)站的可用性監(jiān)控和網(wǎng)站性能監(jiān)控有所區(qū)別,網(wǎng)站可用性監(jiān)控只是簡(jiǎn)單地了解網(wǎng)站是否可用,網(wǎng)站性能監(jiān)控就是要從用戶的角度出發(fā)去深層挖掘從用戶域名解析開始知道用戶數(shù)據(jù)進(jìn)入數(shù)據(jù)庫(kù)的整個(gè)流程中各個(gè)環(huán)節(jié)的性能指標(biāo),也就是現(xiàn)在談得最多的APM。性能監(jiān)控不在本文的討論之中。既然是網(wǎng)站的可用性監(jiān)控,那么部署多個(gè)外網(wǎng)監(jiān)控點(diǎn)就非常有必要,但是一般的公司沒有那么多不同區(qū)域的服務(wù)器,所以沒法從多個(gè)區(qū)域去監(jiān)控網(wǎng)站的可用性,這種情況只能借助于第三方類似監(jiān)控寶這種服務(wù),因?yàn)樗麄冇懈嗟谋O(jiān)控點(diǎn)可以采集到不同的區(qū)域的訪問情況,這個(gè)也是第三方工具的優(yōu)勢(shì)。
Zabbix自帶有Web監(jiān)控服務(wù),但是如果人工批量添加域名監(jiān)控就很麻煩,解決辦法有三個(gè):
1.編寫腳本,直接通過修改數(shù)據(jù)來(lái)批量監(jiān)控域名
2.編寫腳本,通過Zabbix的Web監(jiān)控API來(lái)批量監(jiān)控域名
3.編寫腳本,結(jié)合Zabbix agent或者sender來(lái)批量監(jiān)控域名
二 Zabbix web監(jiān)控相關(guān)源代碼分析
1.分析Zabbix前端php代碼以及SQL語(yǔ)句
Zabbix前端關(guān)于web監(jiān)控的php代碼有:
httpconf.php
httpdetails.php
httpmon.php
include/httptest.inc.php
include/classes/api/managers/CHttpTestManager.php
和web監(jiān)控相關(guān)的MySQL表結(jié)構(gòu)如下:
mysql> desc httpstep; +------------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+---------------------+------+-----+---------+-------+ | httpstepid | bigint(20) unsigned | NO | PRI | NULL | | | httptestid | bigint(20) unsigned | NO | MUL | NULL | | | name | varchar(64) | NO | | | | | no | int(11) | NO | | 0 | | | url | varchar(2048) | NO | | | | | timeout | int(11) | NO | | 15 | | | posts | text | NO | | NULL | | | required | varchar(255) | NO | | | | | status_codes | varchar(255) | NO | | | | | variables | text | NO | | NULL | | | follow_redirects | int(11) | NO | | 1 | | | retrieve_mode | int(11) | NO | | 0 | | | headers | text | NO | | NULL | | +------------------+---------------------+------+-----+---------+-------+ 13 rows in set (0.01 sec)
mysql> desc httpstepitem; +----------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+---------------------+------+-----+---------+-------+ | httpstepitemid | bigint(20) unsigned | NO | PRI | NULL | | | httpstepid | bigint(20) unsigned | NO | MUL | NULL | | | itemid | bigint(20) unsigned | NO | MUL | NULL | | | type | int(11) | NO | | 0 | | +----------------+---------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
mysql> desc httptest; +------------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+---------------------+------+-----+---------+-------+ | httptestid | bigint(20) unsigned | NO | PRI | NULL | | | name | varchar(64) | NO | | | | | applicationid | bigint(20) unsigned | YES | MUL | NULL | | | nextcheck | int(11) | NO | | 0 | | | delay | int(11) | NO | | 60 | | | status | int(11) | NO | MUL | 0 | | | variables | text | NO | | NULL | | | agent | varchar(255) | NO | | Zabbix | | | authentication | int(11) | NO | | 0 | | | http_user | varchar(64) | NO | | | | | http_password | varchar(64) | NO | | | | | hostid | bigint(20) unsigned | NO | MUL | NULL | | | templateid | bigint(20) unsigned | YES | MUL | NULL | | | http_proxy | varchar(255) | NO | | | | | retries | int(11) | NO | | 1 | | | ssl_cert_file | varchar(255) | NO | | | | | ssl_key_file | varchar(255) | NO | | | | | ssl_key_password | varchar(64) | NO | | | | | verify_peer | int(11) | NO | | 0 | | | verify_host | int(11) | NO | | 0 | | | headers | text | NO | | NULL | | +------------------+---------------------+------+-----+---------+-------+ 21 rows in set (0.05 sec)
mysql> desc httptestitem; +----------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+---------------------+------+-----+---------+-------+ | httptestitemid | bigint(20) unsigned | NO | PRI | NULL | | | httptestid | bigint(20) unsigned | NO | MUL | NULL | | | itemid | bigint(20) unsigned | NO | MUL | NULL | | | type | int(11) | NO | | 0 | | +----------------+---------------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
根據(jù)httptestid 刪除httptest相關(guān)的歷史數(shù)據(jù) deleteHistoryByHttpTestIds
現(xiàn)在假設(shè)想要?jiǎng)h除一個(gè)web scenario,查找它的httptestid為14,一個(gè)web scenario對(duì)應(yīng)唯一一個(gè)httptestid
a.先找出所有的itemid
SELECT hti.itemid FROM httptestitem hti WHERE httptestid=14 UNION ALL SELECT hsi.itemid FROM httpstep hs,httpstepitem hsi WHERE hs.httpstepid=hsi.httpstepid AND httptestid=14; +--------+ | itemid | +--------+ | 235244 | | 235245 | | 235246 | | 235247 | | 235248 | | 235249 | +--------+
注意這里使用的UNION ALL而不是UNION,UNION會(huì)去除掉重復(fù)的列,UNION ALL不會(huì)去重。
b.再刪除所有的itemid
調(diào)用deleteHistoryByItemIds函數(shù)
DELETE FROM trends_uint WHERE itemid IN (235244,235245,235246,235247,235248,235249); DELETE FROM history_text WHERE itemid IN (235244,235245,235246,235247,235248,235249); DELETE FROM history_log WHERE itemid IN (235244,235245,235246,235247,235248,235249); DELETE FROM history_uint WHERE itemid IN (235244,235245,235246,235247,235248,235249); DELETE FROM history_str WHERE itemid IN (235244,235245,235246,235247,235248,235249); DELETE FROM history WHERE itemid IN (235244,235245,235246,235247,235248,235249);
這樣這個(gè)httptest的相關(guān)歷史數(shù)據(jù)就清掉了
根據(jù)httptestid查看httptest全部數(shù)據(jù) get_httptest_by_httptestid
SELECT ht.* FROM httptest ht WHERE ht.httptestid=14\G
根據(jù)no查看httpstep get_httpstep_by_no
SELECT hs.* FROM httpstep hs WHERE hs.httptestid=14 AND hs.no=1\G
根據(jù)hostid查看主機(jī)所有的web監(jiān)控 get_httptests_by_hostid
SELECT DISTINCT ht.* FROM httptest ht WHERE hostid=10328\G
這里需要特別說明一下,Zabbix將Template也當(dāng)作一個(gè)host,當(dāng)添加一個(gè)Template的時(shí)候會(huì)自動(dòng)創(chuàng)建一個(gè)hostid,有了這個(gè)hostid才能創(chuàng)建模板內(nèi)的Application,Trigger,Items等
獲取httptests的parent templates
如果是通過模板為主機(jī)創(chuàng)建的Web監(jiān)控,那么主機(jī)的Web監(jiān)控就有個(gè)Parent web scenarios
SELECT ht.httptestid,ht.templateid,ht.hostid,h.name FROM httptest ht INNER JOIN hosts h ON h.hostid=ht.hostid ; +------------+------------+--------+------------------------+ | httptestid | templateid | hostid | name | +------------+------------+--------+------------------------+ | 1 | NULL | 10120 | Template Test URL | | 5 | NULL | 10120 | Template Test URL | | 8 | NULL | 10120 | Template Test URL | | 11 | NULL | 10120 | Template Test URL | | 14 | 1 | 10328 | cdn01 | | 15 | 5 | 10328 | cdn01 | | 16 | 8 | 10328 | cdn01 | | 17 | 11 | 10328 | cdn01 | | 18 | 1 | 10361 | proxy | | 19 | 5 | 10361 | proxy | | 20 | 8 | 10361 | proxy | | 21 | 11 | 10361 | proxy | +------------+------------+--------+------------------------+ 12 rows in set (0.00 sec)
可以看出Template本身也是一個(gè)host,name就是Template的名字,如果其他主機(jī)是使用的Template模板添加Web監(jiān)控,那么就會(huì)有一個(gè)templateid,這個(gè)templateid是屬于Web監(jiān)控這個(gè)Template的
SELECT ht.httptestid,ht.templateid,ht.hostid,h.name FROM httptest ht INNER JOIN hosts h ON h.hostid=ht.hostid ; WHERE httptestid=14;
獲取所有的http step items
SELECT i.value_type,i.valuemapid,i.units,i.itemid,hi.type AS httpitem_type,hs.httpstepid FROM items i,httpstepitem hi,httpstep hs WHERE hi.itemid=i.itemid AND hi.httpstepid=hs.httpstepid AND hs.httptestid=14;
2.分析Zabbix 后端C代碼以及SQL語(yǔ)句
Web監(jiān)控的代碼位于這個(gè)目錄下 src/zabbix_server/httppoller
Zabbix 執(zhí)行Web監(jiān)控的時(shí)候需要libcurl
三 Zabbix批量監(jiān)控Web
1.直接修改數(shù)據(jù)庫(kù)來(lái)批量添加web監(jiān)控
Zabbix數(shù)據(jù)庫(kù)有張ids表,這個(gè)表是用來(lái)記錄存儲(chǔ)各種表當(dāng)前最大的id,下一個(gè)要?jiǎng)?chuàng)建的id就應(yīng)該加1
mysql> desc ids; +------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------+------+-----+---------+-------+ | table_name | varchar(64) | NO | PRI | | | | field_name | varchar(64) | NO | PRI | | | | nextid | bigint(20) unsigned | NO | | NULL | | +------------+---------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
獲取當(dāng)前最大的id
SELECT nextid FROM ids WHERE table_name='httptest' AND field_name='httptestid';
創(chuàng)建一個(gè)新的Web監(jiān)控的httptestid應(yīng)該加1
UPDATE ids SET nextid=nextid+1 WHERE table_name='httptest' AND field_name='httptestid';
創(chuàng)建一個(gè)Web監(jiān)控需要一個(gè)hostid,如果將Web監(jiān)控放到一個(gè)Application下,就需要再創(chuàng)建一個(gè)Application。假設(shè)放到名為URL TEST這個(gè)Application
獲取當(dāng)前最大的applicationid
SELECT nextid FROM ids WHERE table_name='applications' AND field_name='applicationid';
Zabbix的applications表用于存放各個(gè)host的applications
mysql> desc applications; +---------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------------+------+-----+---------+-------+ | applicationid | bigint(20) unsigned | NO | PRI | NULL | | | hostid | bigint(20) unsigned | NO | MUL | NULL | | | name | varchar(255) | NO | | | | | flags | int(11) | NO | | 0 | | +---------------+---------------------+------+-----+---------+-------+ 4 rows in set (0.06 sec)
INSERT INTO applications(applicationid,hostid,name) VALUES(9189,12308,'URL TEST');
這個(gè)hostid為12308的host就創(chuàng)建了一個(gè)名為URL TEST的Application
創(chuàng)建一個(gè)httptest
INSERT INTO httptest(httptestid,name,applicationid,delay,status,variables,agent,authentication,hostid,headers) VALUES(22,'www.baidu.com',9191,30,0,'',"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",0,10328,'');
httptestid 就是查找出ids表中的httptest的nextid加1,需要注意每次插入數(shù)據(jù)之前必須要先查一下這個(gè)ids表,要不然id可能分配錯(cuò)誤
name 就是這個(gè)Web監(jiān)控的名稱,可以直接寫成www.baidu.com 或者 check www.baidu.com 隨便怎么寫
applicationid 需要把這個(gè)Web監(jiān)控放到哪個(gè)Application下,就需要哪個(gè)Application的applicationid
delay 監(jiān)控間隔時(shí)間,默認(rèn)是60s
status 是否開啟監(jiān)控,對(duì)應(yīng)頁(yè)面上顯示就是Enabled或者Disabled,默認(rèn)值是0(Enabled)
variables web監(jiān)控變量設(shè)置,不能為NULL,如果沒有就設(shè)置為空''
agent 使用哪種瀏覽器類型去檢測(cè)
authentication 驗(yàn)證類型,0-none,1-basic HTTP authentication,2-NTLM authentication
獲取當(dāng)前最大的httpstepid
SELECT nextid FROM ids WHERE table_name='httpstep' AND field_name='httpstepid';
然后更新ids的nextid為當(dāng)前值加1
UPDATE ids SET nextid=nextid+1 WHERE table_name='httpstep' AND field_name='httpstepid';
添加一個(gè)httpstep
INSERT INTO httpstep(httpstepid,httptestid,name,no,url,timeout,posts,required,variables,headers,status_codes) VALUES(22,22,'url index',1,'http://www.baidu.com/index.php',60,'','','','',200);
name 是這個(gè)step的名稱
url 是這個(gè)步驟需要訪問的詳細(xì)url
timeout 執(zhí)行步驟的超時(shí)時(shí)間,默認(rèn)是60秒
posts HTTP POST變量
required http響應(yīng)中必須要包含的內(nèi)容
variables step中的變量設(shè)置
headers 設(shè)置HTTP頭
status_codes 允許的HTTP狀態(tài)代碼,例如200,302,404
創(chuàng)建了httptest和httpstep后,下面就要為它們添加相應(yīng)的items,如果不添加Zabbix執(zhí)行Web監(jiān)控的時(shí)候找不到對(duì)應(yīng)的item,就不會(huì)執(zhí)行
httpstep需要3個(gè)item
Download speed web.test.in Bps
Response time web.test.time
Response code web.test.rspcode
httptest需要3個(gè)item
Download speed web.test.in Bps
Failed step web.test.fail
Last error message web.test.error
添加httptest和httpstep的items涉及到4張表
ids
httptestitem
httpstepitem
items
先查出當(dāng)前最大的id,然后再加1
SELECT nextid FROM ids WHERE table_name='items' AND field_name='itemid'; UPDATE ids SET nextid=nextid+1 WHERE table_name='items' AND field_name='itemid';
沒添加一個(gè)item之前重復(fù)執(zhí)行這兩個(gè)SQL語(yǔ)句
INSERT INTO items(itemid, type, hostid, name,description , key_, delay, history, trends, status, value_type, trapper_hosts, units, multiplier, delta, formula, error,logtimefmt, valuemapid, delay_flex, params, data_type, mtime) VALUES (235886, 9, 10328, 'Download speed for step "$2" of scenario "$1".','', 'web.test.in[www.baidu.com,url index,bps]', 60, 30, 90, 0, 0, '', 'Bps', 0, 0, '', '', '',NULL,'', '',0, 0);
INSERT INTO items(itemid, type, hostid, name,description , key_, delay, history, trends, status, value_type, trapper_hosts, units, multiplier, delta, formula, error,logtimefmt, valuemapid, delay_flex, params, data_type, mtime) VALUES (235887, 9, 10328, 'Response time for step "$2" of scenario "$1".','', 'web.test.time[www.baidu.com,url index,resp]', 60, 30, 90, 0, 0, '', 's', 0, 0, '', '', '',NULL,'', '',0, 0)
INSERT INTO items(itemid, type, hostid, name,description , key_, delay, history, trends, status, value_type, trapper_hosts, units, multiplier, delta, formula, error,logtimefmt, valuemapid, delay_flex, params, data_type, mtime) VALUES (235888, 9, 10328, 'Response code for step "$2" of scenario "$1".','', 'web.test.rspcode[www.baidu.com,url index]', 60, 30, 90, 0, 0, '', '', 0, 0, '', '', '',NULL,'', '',0, 0);
查找當(dāng)前最大的httpstepitemid,添加一個(gè)step之前就加1
SELECT nextid FROM ids WHERE table_name='httpstepitem' AND field_name='httpstepitemid'; UPDATE ids SET nextid=nextid+1 WHERE table_name='httpstepitem' AND field_name='httpstepitemid';
INSERT INTO httpstepitem(httpstepitemid, httpstepid, itemid, type) VALUES(64,22,235886,2); INSERT INTO httpstepitem(httpstepitemid, httpstepid, itemid, type) VALUES(65,22,235887,1); INSERT INTO httpstepitem(httpstepitemid, httpstepid, itemid, type) VALUES(66,22,235888,0);
Type of the item.
Possible values:
0 - Zabbix agent;
1 - SNMPv1 agent;
2 - Zabbix trapper;
3 - simple check;
4 - SNMPv2 agent;
5 - Zabbix internal;
6 - SNMPv3 agent;
7 - Zabbix agent (active);
8 - Zabbix aggregate;
9 - web item;
10 - external check;
11 - database monitor;
12 - IPMI agent;
13 - SSH agent;
14 - TELNET agent;
15 - calculated;
16 - JMX agent;
17 - SNMP trap.
Type of information of the item.
Possible values:
0 - numeric float;
1 - character;
2 - log;
3 - numeric unsigned;
4 - text.
Value that will be stored.
Possible values:
0 - (default) as is;
1 - Delta, speed per second;
2 - Delta, simple change.
INSERT INTO items(itemid, type, hostid, name,description , key_, delay, history, trends, status, value_type, trapper_hosts, units, multiplier, delta, formula, error,logtimefmt, valuemapid, delay_flex, params, data_type, mtime) VALUES (235889, 9, 10328, 'Download speed for scenario "$1".','', 'web.test.in[www.baidu.com,,bps]', 60, 30, 90, 0, 0, '', 'Bps', 0, 0, '', '', '',NULL,'', '',0, 0);
INSERT INTO items(itemid, type, hostid, name,description , key_, delay, history, trends, status, value_type, trapper_hosts, units, multiplier, delta, formula, error,logtimefmt, valuemapid, delay_flex, params, data_type, mtime) VALUES (235890, 9, 10328, 'Failed step of scenario "$1".','', 'web.test.fail[www.baidu.com]', 60, 30, 90, 0, 0, '', '', 0, 0, '', '', '',NULL,'', '',0, 0);
INSERT INTO items(itemid, type, hostid, name,description , key_, delay, history, trends, status, value_type, trapper_hosts, units, multiplier, delta, formula, error,logtimefmt, valuemapid, delay_flex, params, data_type, mtime) VALUES (235891, 9, 10328, 'Last error message of scenario "$1".','', 'web.test.error[www.baidu.com]', 60, 30, 90, 0, 0, '', '', 0, 0, '', '', '',NULL,'', '',0, 0);
然后更新httptestitem表
查找當(dāng)前最大的httptestitemid,然后加1
SELECT nextid FROM ids WHERE table_name='httptestitem' AND field_name='httptestitemid'; UPDATE ids SET nextid=nextid+1 WHERE table_name='httptestitem' AND field_name='httptestitemid';
INSERT INTO httptestitem(httptestitemid, httptestid, itemid, type) VALUES(64,22,235889,2); INSERT INTO httptestitem(httptestitemid, httptestid, itemid, type) VALUES(65,22,235890,3); INSERT INTO httptestitem(httptestitemid, httptestid, itemid, type) VALUES(66,22,235891,4);
好了,整個(gè)Web監(jiān)控的數(shù)據(jù)庫(kù)操作就完成了,現(xiàn)在應(yīng)該可以在Zabbix頁(yè)面看到監(jiān)控圖了
假設(shè)有很多網(wǎng)站需要監(jiān)控,每個(gè)網(wǎng)站有不同的URL,現(xiàn)在就可以編寫腳本來(lái)實(shí)現(xiàn)批量監(jiān)控
baidu 1 index.php https://www.baidu.com/index.php baidu 2 index.htm https://www.baidu.com/index.html baidu 3 index.htm https://www.baidu.com/index.htm taobao 1 index https://world.taobao.com/?spm=a21bp.8077467.1417485807582.1.Jx0OM4 taobao 2 login https://world.taobao.com/markets/all/login tencent 1 index http://www.tencent.com/zh-cn/index.shtml tencent 2 about http://www.tencent.com/zh-cn/at/abouttencent.shtml
第一列就是Web scenarios的名稱,第二列是檢查步驟,第三列是執(zhí)行步驟名稱,第四列是檢查的URL,可以根據(jù)自己需要再添加幾列,比如執(zhí)行步驟變量,返回碼等
腳本根據(jù)https://www.zabbix.org/wiki/Docs/howto/script_web_checks 這個(gè)改編而來(lái)
add_web_checks.sh
#!/bin/bash ###### # This script adds WEB checks from a file to a zabbix host by inputting the urls directly into the database." # test on zabbix database 3.0 # written by John Wang on 28/07/2016 # this script is based on https://www.zabbix.org/wiki/Docs/howto/script_web_checks which is not suitable for zabbix 3.0 ### warning !!!!!!! this script can be dangerous as it directly operate the zabbix database,so before execute this script,please ensure that you know this risk!!!!! PROGNAME=$(basename $0) ###### Helper functions function usage { echo "This script adds WEB checks from a file to a zabbix host or multiple hosts in a group by inputting the urls directly into the database." echo "USE WITH CARE!!" echo "" echo "usage: ${PROGNAME}" echo "" echo "MySQL connection options:" echo "-u | --dbuser mysql username to connect to zabbix database (Required)" echo "-p | --dbpass mysql password to connect to zabbix database (Required)" echo "-H | --dbhost hostname of the mysql server (Default: localhost)" echo "-D | --dbname databasename of the zabbix mysql database (Default: zabbix)" echo "" echo "Webcheck options:" echo "-t | --timeout timeout in seconds before a http request times out (Default: 60)" echo "-d | --delay number of seconds between two WEB checks (Default: 60)" echo "-i | --history number of days to keep all values (Default: 90)" echo "-t | --trends number of days to keep trends (Default: 360)" echo "" echo "-o | --hostid the zabbix hostid of the host these WEB checks will be added to(only required one of hostid,groupid and groupname)" echo "-g | --groupid the zabbix hostid of the host these WEB checks will be added to(only required one of hostid,groupid and groupname)" echo "-G | --groupname the zabbix hostid of the host these WEB checks will be added to(only required one of hostid,groupid and groupname)" echo "-A | --appname zabbix application the WEB checks will be added to (Required)" echo "-a | --agent user agent the WEB check will be done as (Default: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)')" echo "" echo "-f | --urlfile file with urls to monitor. One url per line (Required)" echo "--delete delete web checks" echo "" echo "Help:" echo "-h | --help this message" } function error_exit { # ## ---------------------------------------------------------------- ## Function for exit due to fatal program error ## Accepts 1 argument: ## string containing descriptive error message ## Example call of the error_exit function. Note the inclusion ## of the LINENO environment variable. It contains the current ## line number. ## # error_exit "$LINENO: An error has occurred." ## ## ---------------------------------------------------------------- echo -e "\e[035m${PROGNAME}: ${1:-"Unknown Error"}\e[0m" 1>&2 exit 1 } # function log { echo -e "\e[035m${PROGNAME}: $1\e[0m" } # ##### Parameter processing # These are the host and hostgroup that all web checks will be added to HOSTID= GROUPID= GROUPNAME= APPLNAME= USERAGENT='Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)' HOSTFILE= DELETE=false # timeout in seconds before a http request times out TIMEOUT=60 # number of seconds between two checks DELAY=60 # number of days to keep all values HISTORY=90 # number of days to keep trends TRENDS=360 MYSQLUSER=zabbix MYSQLPASS= MYSQLHOST= MYSQLDB=zabbix while [ "$1" != "" ]; do case $1 in -u | --dbuser ) shift MYSQLUSER=$1 ;; -p | --dbpass ) shift MYSQLPASS=$1 ;; -H | --dbhost ) shift MYSQLHOST=$1 ;; -D | --dbname ) shift MYSQLDB=$1 ;; -t | --timeout ) shift TIMEOUT=$1 ;; -d | --delay ) shift DELAY=$1 ;; -i | --history ) shift HISTORY=$1 ;; -t | --trends ) shift TRENDS=$1 ;; -o | --hostid ) shift HOSTID=$1 ;; -g | --groupid ) shift GROUPID=$1 ;; -G | --groupname ) shift GROUPNAME=$1 ;; -f | --urlfile ) shift HOSTFILE=$1 ;; --delete ) shift DELETE=$1 ;; -a | --agent ) shift USERAGENT=$1 ;; -A | --appname ) shift APPLNAME=$1 ;; -h | --help ) usage exit ;; * ) usage exit 1 esac shift done if [ -z "$MYSQLUSER" ] then error_exit "$LINENO: Required parameter not found: -u | --dbuser. See -h | --help for more information" fi if [ -z "$MYSQLPASS" ] then error_exit "$LINENO: Required parameter not found: -p | --dbpass. See -h | --help for more information" fi if [ -z "$HOSTFILE" ] then error_exit "$LINENO: Required parameter not found: -f | --urlfile. See -h | --help for more information" fi if [ -z "$APPLNAME" ] then error_exit "$LINENO: Required parameter not found: -A | --appname. See -h | --help for more information" fi # Executes an sql command, call like so: # mysql_cmd "select * from zabbix.items where hostid = ${HOSTID}" mysql_cmd () { if [ -z "$1" ] # Is parameter #1 zero length? then error_exit "-Parameter #1 is zero length in mysql_cmd.-\n" # Or no parameter passed. fi # --skip-columna-names for parseable output assumes we don't use select * # so you already know which column names are in which order # --batch output results in record-per-line tab-delimited way #log "mysql -u${MYSQLUSER} -p${MYSQLPASS} -D${MYSQLDB} -h${MYSQLHOST} --batch --skip-column-name -e\"$1\"" result=`mysql -u"${MYSQLUSER}" -p"${MYSQLPASS}" -D${MYSQLDB} -h${MYSQLHOST} --batch --skip-column-name -e"$1"` if [ "$?" = "0" ]; then # in bash you cannot return a value so we echo it, the calling function can # then just capture that using `` or $() echo $result else error_exit "MySQL command failed: $1 (-u${MYSQLUSER} -p${MYSQLPASS} -D${MYSQLDB} -h${MYSQLHOST})" fi } zabbix_get_next_id () { table="$1" field="$2" # Get the current id and the next id and compare them. If the difference is 1, use it. 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 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 } ########### add triggers ### only add trigger for response code function add_web_trigger() { itemid="$1" key_="$2" description="$3" url="$4" ##### check trigger functionid=$(mysql_cmd "select functionid from functions where itemid=${itemid} and function='last' and parameter='#3' ") if [ -z "$functionid" ];then echo "functionid not exists!" echo "creating trigger '$description' on $HOSTID" functionid=$(zabbix_get_next_id 'functions' 'functionid') triggerid=$(zabbix_get_next_id 'triggers' 'triggerid') expression="{$functionid}<>200" #echo $expression mysql_cmd "insert into triggers(triggerid,expression,description,url,priority,comments) values (${triggerid},'${expression}','${description}','${url}',4,'')" mysql_cmd "insert into functions(functionid,itemid,triggerid,function,parameter) values (${functionid},${itemid},${triggerid},'last','#3')" else expression="{$functionid}<>200" triggerid=$(mysql_cmd "select triggerid from functions where functionid=${functionid}") trigger_expression=$(mysql_cmd "select expression from triggers where triggerid=${triggerid}") trigger_description=$(mysql_cmd "select description from triggers where triggerid=${triggerid}") #echo $trigger_expression #echo $expression if [ "$trigger_expression" != "$expression" -o "$trigger_description" != "$description" ];then echo "trigger expression is not right,delete it " mysql_cmd "delete from triggers where triggerid=${triggerid}" echo "creating trigger '$description' on $HOSTID" mysql_cmd "insert into triggers(triggerid,expression,description,url,priority,comments) values (${triggerid},'${expression}','${description}','${url}',4,'')" else echo "trigger for ${key_} already exists!" fi fi } ##################### ADD WEB CHECK FOR HOST ######################## function add_web_check_for_host() { # Add an application if it doesn't exist and get it's appid returned app_id=`mysql_cmd "select applicationid from applications where hostid = '${HOSTID}' and name = '${APPLNAME}'"` if [ -z "$app_id" ] then app_id=`zabbix_get_next_id 'applications' 'applicationid'` mysql_cmd "insert into applications(applicationid, hostid, name) values (${app_id}, ${HOSTID}, '${APPLNAME}')" fi # Loop through all sites to be added while read line; do ###### ### baidu 1 index.php https://www.baidu.com/index.php #### log "== Starting ${line}" site_name=$(echo $line|awk '{print $1}') step_no=$(echo $line|awk '{print $2}') url_name=$(echo $line|awk '{print $3}') step_url=$(echo $line|awk '{print $4}') test_name="check website ${site_name} on {HOST.NAME}" step_name="check url ${url_name}" ####### for adding trigger key_="web.test.rspcode[${test_name},${step_name}]" description="${site_name} url ${url_name} on {HOST.NAME} is down" # Add an httptest if it doesn't exist and get it's httptestid returned httptest_id=`mysql_cmd "select httptestid from httptest where hostid='${HOSTID}' and name='${test_name}'"` if [ -z "$httptest_id" ];then httptest_id=`zabbix_get_next_id 'httptest' 'httptestid'` mysql_cmd "INSERT INTO httptest(httptestid,name,applicationid,delay,status,variables,agent,authentication,hostid,headers) VALUES(${httptest_id},'${test_name}',${app_id},60,0,'','${USERAGENT}',0,${HOSTID},'')" fi # Add an httpstep if it doesn't exist and get it's httpstepid returned httpstep_id=`mysql_cmd "select httpstepid from httpstep where httptestid='${httptest_id}' and no='${step_no}'"` if [ -z "$httpstep_id" ];then httpstep_id=`zabbix_get_next_id 'httpstep' 'httpstepid'` mysql_cmd "INSERT INTO httpstep(httpstepid,httptestid,name,no,url,timeout,posts,required,variables,headers,status_codes) VALUES(${httpstep_id},${httptest_id},'${step_name}',${step_no},'${step_url}',${TIMEOUT},'','','','',200)" else stepname=`mysql_cmd "select name from httpstep where httpstepid=${httpstep_id}"` stepurl=`mysql_cmd "select url from httpstep where httpstepid=${httpstep_id}"` itemids=$(mysql_cmd "SELECT hsi.itemid FROM httpstep hs,httpstepitem hsi,items i WHERE hsi.itemid=i.itemid and hs.httpstepid=hsi.httpstepid and hs.httpstepid=${httpstep_id} ") if [ -z "$(echo $itemids|grep ' ')" ];then ##### only one item itemids1=$itemids else itemids1=$(echo $itemids|sed -n 's/ \|\t/,/gp') fi triggerids=$(mysql_cmd "select triggerid from functions where itemid in ($(echo $itemids1))") if [ -z "$(echo $triggerids|grep ' ')" ];then ##### only one item triggerids1=$triggerids else triggerids1=$(echo $triggerids|sed -n 's/ \|\t/,/gp') fi eventids=$(mysql_cmd "select eventid from escalations where itemid i
名稱欄目:使用Zabbix批量監(jiān)控網(wǎng)站可用性方案一
本文地址:http://weahome.cn/article/ipjgcj.html