3、zabbix_get和zabbix_sender命令的使用
Zabbix_get命令:
zabbix_get是zabbix中的一个程序,用于zabbix-server到zabbix-agent获取数据。通常用来检测agent的配置是否正确。
1
2
3
4
5
6
7
8
9
10
11
|
[[email protected] ~] # zabbix_get -h
Zabbix get v2.2.11 (revision 56693) (12 November 2015) usage: zabbix_get [-hV] -s <host name or IP> [-p <port>] [-I <IP address>] -k <key> Options: -s --host <host name or IP> Specify host name or IP address of a host
-p --port <port number> Specify port number of agent running on the host. Default is 10050
-I -- source -address <IP address> Specify source IP address
-k --key <key of metric> Specify key of item to retrieve value for
-h --help Give this help
-V --version Display version number
Example: zabbix_get -s 127.0.0.1 -p 10050 -k "system.cpu.load[all,avg1]"
|
参数说明:
-s --host: 指定客户端主机名或者IP
-p --port:客户端端口,默认10050
-I --source-address:指定源IP,写上zabbix server的ip地址即可,一般留空,服务器如果有多ip的时候,你指定一个。
-k --key:你想获取的key
至于使用长参数还是短的,自己选,我经常使用-s而不是--host,太长了。
获取CPU负载
1
2
|
[[email protected] ~] # zabbix_get -s 127.0.0.1 -p 10050 -k "system.cpu.load[all,avg15]"
0.000000 |
key可以加引号,也可以不加。
获取主机名
1
2
|
[[email protected] ~] # zabbix_get -s 127.0.0.1 -p 10050 -k system.hostname
localhost.localdomain |
Zabbix_sender命令:
zabbix_sender是什么?有什么作用?
zabbix获取key值有超时时间,如果自定义的key脚本一般需要执行很长时间,这根本没法去做监控,那怎么办呢?使用zabbix监控类型zabbix trapper,需要配合zabbix_sender给它传递数据。关于trapper的用法,我们来弄个实例。
执行超长时间脚本,如:脚本去几十台服务器拉去数据,每个日志都上G,然后日志整合在一起,统计出返回值。这种脚本比如超时,所以我们必须改成让客户端提交数据的方式。
zabbix_sender命令用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[[email protected] ~] # zabbix_sender -h
Zabbix Sender v2.2.11 (revision 56693) (12 November 2015) usage: zabbix_sender [-Vhv] {[-zpsI] -ko | [-zpI] -T -i < file > -r} [-c < file >]
Options: -c --config < file > Absolute path to the configuration file
-z --zabbix-server <server> Hostname or IP address of Zabbix server
-p --port <server port> Specify port number of server trapper running on the server. Default is 10051
-s --host < hostname > Specify host name. Host IP address and DNS name will not work
-I -- source -address <IP address> Specify source IP address
-k --key <key>
Specify item key
-o --value <key value> Specify value
-i --input- file <input file > Load values from input file . Specify - for standard input
Each line of file contains whitespace delimited: < hostname > <key> <value>
Specify - in < hostname > to use hostname from configuration file or --host argument
-T --with-timestamps Each line of file contains whitespace delimited: < hostname > <key> <timestamp> <value>
This can be used with --input- file option
Timestamp should be specified in Unix timestamp format
-r --real- time Send metrics one by one as soon as they are received
This can be used when reading from standard input
- v --verbose Verbose mode, -vv for more details
Other options: -h --help Give this help
-V --version Display version number
|
使用参数:
1
2
3
4
5
6
7
8
9
10
11
|
-c --config < file > 配置文件绝对路径
-z --zabbix-server <server> zabbix server的IP地址
-p --port <server port> zabbix server端口.默认10051
-s --host < hostname > 主机名,zabbix里面配置的主机名(不是服务器的 hostname ),不能使用ip地址
-I -- source -address <IP address> 源IP
-k --key <key> 监控项的key
-o --value <key value> key值
-i --input- file <input file > 从文件里面读取 hostname 、key、value 一行为一条数据,使用空格作为分隔符,如果主机名带空格,那么请使用双引号包起来
-T --with-timestamps 一行一条数据,空格作为分隔符: < hostname > <key> <timestamp> <value>,配合 --input- file option,timestamp为unix时间戳
-r --real- time 将数据实时提交给服务器
- v --verbose 详细模式, -vv 更详细
|
Zabbix_sender使用实例:
先在主机zabbix server 上创建一个key。
传递一个数据到item中,注意-s后面为zabbix_agent.conf文件中配置的hostname,不是IP地址,也不是服务器的主机名。
1
2
3
|
[[email protected] alertscripts] # zabbix_sender -s "ZabbixServer" -z 127.0.0.1 -k "key.test.trapper" -o 1 -r
info from server: "processed: 1; failed: 0; total: 1; seconds spent: 0.000034"
sent: 1; skipped: 0; total: 1 |
failed为0,表示传送失败的为0个。
zabbix_sender还可以批量上传数据,即将服务器的hostname(zabbix_agent.conf文件中配置的主机名)、key、value三个值保存到文件中,然后将该文件中的数据上传到各个item中。
zabbix_sender批量传递key值
1
2
3
4
5
6
7
8
9
|
#cat f.txt "Zabbix server" ttlsa.trapper 10
"Zabbix server" ttlsa.trapper 20
"Zabbix server" ttlsa.trapper 30
"Zabbix server" ttlsa.trapper 40
"Zabbix server" ttlsa.trapper 1
# ./zabbix_sender -z 127.0.0.1 -i f.txt info from server: "processed: 5; failed: 0; total: 5; seconds spent: 0.000085"
sent: 5; skipped: 0; total: 5 |
每行对应一个key值,一般是相同的主机名、不同的key、不同的key值。这边方便测试,所以都用了同一个key
这种方式也是zabbix trapper监控方式的模型。
Zabbix Trapper监控案例演示:
创建两个监控项,monitor[TotalMemory]和monitor[FreeMemory],分别用来监控总内存和剩余内存。
监控脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash hostname =$1
zabbix_server_ip=172.16.206.130 TotalMemory() { TotalMemory=` cat /proc/meminfo | awk '/^MemTotal:/{print $2}' `
echo "$hostname monitor[TotalMemory] $TotalMemory" } FreeMemory() { FreeMemory=` cat /proc/meminfo | awk '/^MemFree:/{print $2}' `
echo "$hostname monitor[FreeMemory] $FreeMemory"
} TotalMemory >> /tmp/test .txt
FreeMemory >> /tmp/test .txt
zabbix_sender -z $zabbix_server_ip -i /tmp/test .txt &> /dev/null
if [ ` echo $?` - eq 0 ]; then
echo 0
else echo 1
fi rm -rf /tmp/test .txt
|
注意:脚本最后一行是当数据上传完成后删除保存数据的文件,这样下次执行脚本时保证不会有相同的key对应多个value。
zabbix web上创建对应的item
注意:上面我们创建了item,也创建了获取item值的脚本。但是这个脚本不会自动执行,将数据传送到zabbix server上。所以我们还需要再创建一个监控项,这个监控项会定时执行我们创建的脚本。
创建自定义监控的conf文件
1
2
|
[[email protected] zabbix_agentd.d] # vim userparameter_memory.conf
UserParameter=send. date [*], /etc/zabbix/scripts/memory .monitor.sh $1
|
zabbix server上创建监控项: