优先参考 zabbix自动发现与监控内存和CPU使用率最高的进程

http://7424593.blog.51cto.com/7414593/1908930

  1. 监控网站url code

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
#!/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 
-
"^#"
)) 
        
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 
-
/
dev
/
null 
-
-
%
{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.nginx统一监控脚本  Active

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
#!/bin/bash
#
# Filename:    nginx_monitor.sh
# Revision:    1.0
# Date:        2014/09/24
# Author:      Qicheng
# Email:
# Website:     http://qicheng0211.blog.51cto.com
# Description: nginx统一监控脚本
# Notes:       
#
  
# 修改AGENT_CONF的值为本地zabbix agent的配置文件路径
AGENT_CONF
=
"/usr/local/zabbix/etc/zabbix_agentd.conf"
# nginx站点的配置文件路径
NGINX_SITE_CONF
=
"/usr/local/zabbix/scripts/nginx_site.conf"
# zabbix_sender的路径
ZBX_SENDER
=
"/usr/local/bin/zabbix_sender"
  
FUNCTION
=
$
1
HOST_NAME
=
$
2
NGINX_SITE
=
$
3
CURL
=
"/usr/bin/curl"
TIMEOUT
=
30
  
# nginx site low-level discovery
function nginxSiteDiscovery()
{
    
nginx_site
=
($(grep 
'^[^#]' 
${NGINX_SITE_CONF}))
    
max_index
=
$[${
#nginx_site[@]}-1]
    
printf 
'{\n'
    
printf 
'\t"data":['
    
for 
key 
in 
`seq 
-
s
' ' 
0 
$max_index`
    
do
        
printf 
'\n\t\t{'
        
printf 
"\"{#NGINX_SITE}\":\"${nginx_site[${key}]}\"}"
        
if 
[ $key 
-
ne $max_index ];then
            
printf 
","
        
fi
    
done
    
printf 
'\n\t]\n'
    
printf 
'}\n'
}
  
# 获取nginx status,把数据发送到zabbix server
function getNginxStatus()
{
    
nginx_status_url
=
"${NGINX_SITE}/nginx_status"
    
# 获取nginx_status后,保存到下面的文件里
    
nginx_status_file
=
"/tmp/nginx_status_$(echo ${NGINX_SITE} | sed 's#^http.*://##; s#/#_#g').log"
    
:>
"$nginx_status_file"
  
    
# curl获取nginx_status
    
${CURL} 
-
-
-
connect
-
timeout ${TIMEOUT} 
"$nginx_status_url" 
2
>&
1 
| tee 
"$nginx_status_file"
    
line_num
=
$(cat 
"$nginx_status_file" 
| wc 
-
l)
    
# 判断是否正确获取nginx_status
    
[ $line_num 
-
ne 
4 
] && { echo 
"ERROR: $nginx_status_file is not correct."
; exit 
1
;}
  
    
active
=
$(cat 
"$nginx_status_file" 
| grep 
'Active' 
| awk 
'{print $NF}'
)
    
reading
=
$(cat 
"$nginx_status_file" 
| grep 
'Reading' 
| awk 
'{print $2}'
)
    
writing
=
$(cat 
"$nginx_status_file" 
| grep 
'Writing' 
| awk 
'{print $4}'
)
    
waiting
=
$(cat 
"$nginx_status_file" 
| grep 
'Waiting' 
| awk 
'{print $6}'
)
    
accepts
=
$(cat 
"$nginx_status_file" 
| awk NR
=
=
3 
| awk 
'{print $1}'
)
    
handled
=
$(cat 
"$nginx_status_file" 
| awk NR
=
=
3 
| awk 
'{print $2}'
)
    
requests
=
$(cat 
"$nginx_status_file" 
| awk NR
=
=
3 
| awk 
'{print $3}'
)
    
echo 
"Sending the data to zabbix server..."
    
# 将特定格式的数据发送到zabbix server,每行的格式为:<hostname> <key> <value>
    
cat << EOF | ${ZBX_SENDER} 
-
c ${AGENT_CONF} 
-
-
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,active]" 
"${active}"
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,reading]" 
"${reading}"
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,writing]" 
"${writing}"
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,waiting]" 
"${waiting}"
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,accepts]" 
"${accepts}"
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,handled]" 
"${handled}"
"${HOST_NAME}" 
"nginx_status[$NGINX_SITE,requests]" 
"${requests}"
EOF
}
  
[ $
# -eq 0 ] && { echo "ERROR: The script needs at least one parameter."; exit 1;}
  
case $FUNCTION 
in
    
nginxSiteDiscovery|getNginxStatus)
        
$FUNCTION
        
;;
    
*
)
        
echo 
"ERROR: Bad parameters."
        
exit 
1
        
;;
esac

3.分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
#Fucation:zabbix low-level discovery
partition() {
            
port
=
($(grep 
-
"(vd[a-z]$|sd[a-z]$)" 
/
proc
/
partitions|awk 
'{print $4}'
))
            
printf 
'{\n'
            
printf 
'\t"data":[\n'
               
for 
key 
in 
${!port[@]}
                   
do
                       
if 
[[ 
"${#port[@]}" 
-
gt 
1 
&& 
"${key}" 
-
ne 
"$((${#port[@]}-1))" 
]];then
                          
printf 
'\t {\n'
                          
printf 
"\t\t\t\"{#PARTITIONNAME}\":\"${port[${key}]}\"},\n"
                     
else 
[[ 
"${key}" 
-
eq 
"((${#port[@]}-1))" 
]]
                          
printf 
'\t {\n'
                          
printf 
"\t\t\t\"{#PARTITIONNAME}\":\"${port[${key}]}\"}\n"
                       
fi
               
done
                          
printf 
'\t ]\n'
                          
printf 
'}\n'
}
$
1

sh /usr/local/zabbix/bin/partition_low_discovery.sh partition

4.io 

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
[root@xx zabbix_agentd.conf.d]
# cat vfsiojohn.conf 
UserParameter
=
vfsiojohn.discovery,
/
usr
/
bin
/
python 
/
usr
/
local
/
zabbix
/
bin
/
disk_discovery.py
UserParameter
=
disk.status[
*
],
/
usr
/
local
/
zabbix
/
bin
/
disk_status.sh $
1 
$
2
[root@xx zabbix_agentd.conf.d]
# /usr/bin/python /usr/local/zabbix/bin/disk_discovery.py
{
    
"data"
:[
        
{
            
"{#DISK_NAME}"
:
"sda"
        
}
    
]
}
[root@xx zabbix_agentd.conf.d]
# cat /usr/local/zabbix/bin/disk_status.sh
#/bin/sh
  
device
=
$
1
item
=
$
2
  
case $item 
in
         
rrqm)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b"
|tail 
-
1
|awk 
'{print $2}'
            
;;
         
wrqm)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b"
|tail 
-
1
|awk 
'{print $3}'
            
;;
          
rps)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b"
|tail 
-
1
|awk 
'{print $4}'
            
;;
          
wps)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $5}'
            
;;
        
rKBps)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $6}'
            
;;
        
wKBps)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $7}'
            
;;
     
avgrq
-
sz)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $8}'
            
;;
     
avgqu
-
sz)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $9}'
            
;;
        
await)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $10}'
            
;;
        
svctm)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $11}'
            
;;
         
util)
            
/
usr
/
bin
/
tail 
-
n20 
/
tmp
/
iostat_output |grep 
"\b$device\b" 
|tail 
-
1
|awk 
'{print $12}'
            
;;
esac

5.zabbix 监控内存和cpu占用最多的进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# cat discovery_process.sh 
#!/bin/bash
#system process discovery script
top 
-
-
1 
/
tmp
/
.top.txt && chown zabbix. 
/
tmp
/
.top.txt
proc_array
=
(`tail 
-
+
8 
/
tmp
/
.top.txt | awk 
'{a[$NF]+=$10}END{for(k in a)print a[k],k}'
|sort 
-
gr|head 
-
10
|cut 
-
d
" " 
-
f2`)
length
=
${
#proc_array[@]}
   
printf 
"{\n"
printf 
'\t'
"\"data\":["
for 
((i
=
0
;i<$length;i
+
+
))
do
    
printf 
"\n\t\t{"
    
printf 
"\"{#PROCESS_NAME}\":\"${proc_array[$i]}\"}"
    
if 
[ $i 
-
lt $[$length
-
1
] ];then
        
printf 
","
    
fi
done
    
printf 
"\n\t]\n"
printf 
"}\n"

or  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cat discovery_process2.sh 
#!/bin/bash
#system process discovery script
top 
-
-
1 
/
tmp
/
.top.txt && chown zabbix. 
/
tmp
/
.top.txt
proc_array
=
`tail 
-
+
8 
/
tmp
/
.top.txt | awk 
'{a[$NF]+=$10}END{for(k in a)print a[k],k}'
|sort 
-
gr|head 
-
10
|cut 
-
d
" " 
-
f2`
   
length
=
`echo 
"${proc_array}" 
| wc 
-
l`
count
=
0
echo 
'{'
echo 
-
'\t"data":['
echo 
"$proc_array" 
while 
read line
do
    
echo 
-
en 
'\t\t{"{#PROCESS_NAME}":"'
$line
'"}'
    
count
=
$(( $count 
+ 
1 
))
    
if 
[ $count 
-
lt $length ];then
        
echo 
','
    
fi
done
echo 
-
'\n\t]'
echo 
'}'

6.zabbix 监控io占用最多的进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cat pidtest.sh
#!/bin/bash
#Fucation:mysql low-level discovery
#Script_name mysql_low_discovery.sh
pidtest() {
            
port
=
($(cat 
/
tmp
/
pidsta|awk 
'{print $2}'
))
            
printf 
'{\n'
            
printf 
'\t"data":[\n'
               
for 
key 
in 
${!port[@]}
                   
do
                       
if 
[[ 
"${#port[@]}" 
-
gt 
1 
&& 
"${key}" 
-
ne 
"$((${#port[@]}-1))" 
]];then
                          
printf 
'\t {\n'
                          
printf 
"\t\t\t\"{#PIDSTA}\":\"${port[${key}]}\"},\n"
                     
else 
[[ 
"${key}" 
-
eq 
"((${#port[@]}-1))" 
]]
                          
printf 
'\t {\n'
                          
printf 
"\t\t\t\"{#PIDSTA}\":\"${port[${key}]}\"}\n"
                       
fi
               
done
                          
printf 
'\t ]\n'
                          
printf 
'}\n'
}
$
1