1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
1.
第一种 目前用的
[root@node zabbix_agentd.conf.d]
# cat web_site_discovery.conf
UserParameter
=
web.site.discovery,
/
usr
/
local
/
zabbix
/
scripts
/
web_site_code_status web_site_discovery
UserParameter
=
web.site.code[
*
],
/
usr
/
local
/
zabbix
/
scripts
/
web_site_code_status web_site_code $
1
cat
/
usr
/
local
/
zabbix
/
scripts
/
web_site_code_status
#!/bin/bash
# function:monitor tcp connect status from zabbix
# License: GPL
# mail:itnihao@qq.com
# version:1.0 date:2012-12-09
source
/
etc
/
bashrc >
/
dev
/
null
2
>&
1
source
/
etc
/
profile >
/
dev
/
null
2
>&
1
#/usr/bin/curl -o /dev/null -s -w %{http_code} http://$1/
WEB_SITE_discovery () {
WEB_SITE
=
($(cat
/
usr
/
local
/
zabbix
/
scripts
/
WEB.txt|grep
-
v
"^#"
))
printf
'{\n'
printf
'\t"data":[\n'
for
((i
=
0
;i<${
#WEB_SITE[@]};++i))
{
num
=
$(echo $((${
#WEB_SITE[@]}-1)))
if
[
"$i"
!
=
${num} ];
then
printf
"\t\t{ \n"
printf
"\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$i]}\"},\n"
else
printf
"\t\t{ \n"
printf
"\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$num]}\"}]}\n"
fi
}
}
web_site_code () {
/
usr
/
bin
/
curl
-
o
/
dev
/
null
-
s
-
w
%
{http_code} http:
/
/
$
1
}
case
"$1"
in
web_site_discovery)
WEB_SITE_discovery
;;
web_site_code)
web_site_code $
2
;;
*
)
echo
"Usage:$0 {web_site_discovery|web_site_code [URL]}"
;;
esac
2.
第二种 监控网站访问时间
[root@node zabbix_agentd.conf.d]
# cat web_status.conf
UserParameter
=
get_web_name,
/
usr
/
local
/
zabbix
/
bin
/
web_monitor.py web_name_discovery
UserParameter
=
get_web_status[
*
],
/
usr
/
local
/
zabbix
/
bin
/
web_monitor.py get_web_status $
1
$
2
cat
/
usr
/
local
/
zabbix
/
bin
/
web_monitor.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
os,sys
import
time
import
sys
import
pycurl
import
json
urllist
=
[
'a.com'
,
'b.com'
]
class
Test:
def
__init__(
self
):
self
.contents
=
''
def
callback(
self
,curl):
self
.contents
=
self
.contents
+
curl
def
test_gzip(url):
data
=
{}
t
=
Test()
c
=
pycurl.Curl()
c.setopt(pycurl.WRITEFUNCTION,t.callback)
c.setopt(pycurl.ENCODING,
'gzip'
)
c.setopt(pycurl.URL,url)
c.perform()
data[
'HTTP_CODE'
]
=
c.getinfo(c.HTTP_CODE)
data[
'NAMELOOKUP_TIME'
]
=
(c.getinfo(c.NAMELOOKUP_TIME))
*
1000
data[
'CONNECT_TIME'
]
=
(c.getinfo(c.CONNECT_TIME))
*
1000
data[
'PRETRANSFER_TIME'
]
=
(c.getinfo(c.PRETRANSFER_TIME))
*
1000
data[
'SPEED_DOWNLOAD'
]
=
c.getinfo(c.SPEED_DOWNLOAD)
return
data
def
web_name_discovery():
web_list
=
[]
web_dict
=
{
"data"
:
None
}
for
url
in
urllist:
url_dict
=
{}
url_dict[
"{#NAME}"
]
=
url
web_list.append(url_dict)
web_dict[
"data"
]
=
web_list
jsonStr
=
json.dumps(web_dict, sort_keys
=
True
, indent
=
4
)
return
jsonStr
def
get_web_status():
data
=
test_gzip(sys.argv[
2
])
return
data[sys.argv[
3
]]
if
__name__
=
=
'__main__'
:
if
sys.argv[
1
]
=
=
"web_name_discovery"
:
print
web_name_discovery()
elif
sys.argv[
1
]
=
=
"get_web_status"
:
print
get_web_status()
3.
第三种
[root@node zabbix_agentd.conf.d]
# cat warn_status.conf
UserParameter
=
get_warn_name,
/
usr
/
local
/
zabbix
/
bin
/
warn_web.py
UserParameter
=
get_warn_status[
*
],
/
usr
/
local
/
zabbix
/
bin
/
warn_monitor.py $
1
cat
/
usr
/
local
/
zabbix
/
bin
/
warn_web.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
subprocess
import
json
args
=
'''awk -F' ' '{print $1}' /usr/local/zabbix/scripts/warn.txt|grep -v "#"'''
t
=
subprocess.Popen(args, shell
=
True
, stdout
=
subprocess.PIPE).communicate()[
0
]
lwarns
=
[]
for
mongo
in
t.split(
'\n'
):
if
len
(mongo) !
=
0
:
lwarns.append({
'{#WARN_HOST}'
: mongo})
print
json.dumps({
'data'
: lwarns}, indent
=
4
, separators
=
(
','
,
':'
))
cat
/
usr
/
local
/
zabbix
/
bin
/
warn_monitor.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
urllib2,sys
import
json
if
len
(sys.argv)
=
=
2
:
argv1
=
sys.argv[
1
]
def
http_get():
url
=
argv1
# 页面的地址
response
=
urllib2.urlopen(url)
# 调用urllib2向服务器发送get请求
return
response.read()
# 获取服务器返回的页面信息
ret
=
http_get()
retj
=
json.loads(ret)
if
retj[
'action'
]
=
=
"NONE"
:
print
0
else
:
print
1
4.
配置>主机(需联网)>web场景>创建web场景
5.shell
+
nc
+
namp等工具
+
crontab
#!/bin/bash
#this is a jiankong qutouwang bash
nc
-
z
-
v
-
w
10
80
&>
/
dev
/
null
if
[ $?
-
ne
0
]
then nmap www.xxx.com
-
p
80
|grep
'80/tcp open http'
if
[ $?
-
ne
0
]
then
echo
" is already stopd"
| mail
-
s
"www.xxx.com "
renzhiyuan666666@vip.qq.com
fi
fi
#https
nc
-
z
-
v
-
w
10
443
&>
/
dev
/
null
if
[ $?
-
ne
0
]
then nmap
-
p
443
|grep
'443/tcp open https'
if
[ $?
-
ne
0
]
then
echo
"www.xxx.com https is already stopd"
| mail
-
s
" "
renzhiyuan666666@vip.qq.com
fi
fi
|
后面两种很实用。
本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1961209,如需转载请自行联系原作者