从zabbix数据库获取指标数据进行监控告警

 

1.查找hostid

mysql> select * from hosts where host='hxl-elasticsearch-prd';
+--------+--------------+------------------------------------------------+--------+---------------+-------+-----------+-------------+------------+---------------+----------------+---------------+---------------+--------------------+----------------+--------------------+----------------+---------------+--------------------+------------------+------------------+------------------+------------------+------------+------------+-------------------+---------------+-----------------+-----------+------------------------------------------------+-------+------------+-------------+-------------+------------+------------+-------------+------------------+---------+---------------+---------------+
| hostid | proxy_hostid | host                                           | status | disable_until | error | available | errors_from | lastaccess | ipmi_authtype | ipmi_privilege | ipmi_username | ipmi_password | ipmi_disable_until | ipmi_available | snmp_disable_until | snmp_available | maintenanceid | maintenance_status | maintenance_type | maintenance_from | ipmi_errors_from | snmp_errors_from | ipmi_error | snmp_error | jmx_disable_until | jmx_available | jmx_errors_from | jmx_error | name                                           | flags | templateid | description | tls_connect | tls_accept | tls_issuer | tls_subject | tls_psk_identity | tls_psk | proxy_address | auto_compress |
+--------+--------------+------------------------------------------------+--------+---------------+-------+-----------+-------------+------------+---------------+----------------+---------------+---------------+--------------------+----------------+--------------------+----------------+---------------+--------------------+------------------+------------------+------------------+------------------+------------+------------+-------------------+---------------+-----------------+-----------+------------------------------------------------+-------+------------+-------------+-------------+------------+------------+-------------+------------------+---------+---------------+---------------+
|  10664 |         NULL | yeemiao-elasticsearch-prd |      0 |             0 |       |         1 |           0 |          0 |            -1 |              2 |               |               |                  0 |              0 |                  0 |              0 |          NULL |                  0 |                0 |                0 |                0 |                0 |            |            |                 0 |             0 |               0 |           | yeemiao-elasticsearch-c099aef-prd-172017010020 |     0 |       NULL |             |           1 |          1 |            |             |                  |         |               |             1 |
+--------+--------------+------------------------------------------------+--------+---------------+-------+-----------+-------------+------------+---------------+----------------+---------------+---------------+--------------------+----------------+--------------------+----------------+---------------+--------------------+------------------+------------------+------------------+------------------+------------+------------+-------------------+---------------+-----------------+-----------+------------------------------------------------+-------+------------+-------------+-------------+------------+------------+-------------+------------------+---------+---------------+---------------+
1 row in set (0.00 sec)

 

2.查找监控配置项

mysql> select itemid,name from items where hostid=10664;
+--------+----------------------------------------------+
| itemid | name                                         |
+--------+----------------------------------------------+
|  66283 | IO_Stats Sender Data Status                  |
|  67791 | eth0 Lost Package Percentage                 |
|  67632 | {#NET_DEV_NAME} Lost Package Percentage      |
|  67631 | Net Lost  Package Sender Data Status         |
|  64243 | Md5sum of /etc/login.defs                    |
|  64242 | Md5sum of /etc/pam.d/system-auth             |
|  64236 | Md5sum of /etc/profile                       |
|  64241 | Md5sum of /etc/ssh/sshd_config               |
|  64239 | Md5sum of /home/yeemiao/.ssh/authorized_keys |
|  64240 | Md5sum of /root/.ssh/authorized_keys         |
|  64234 | Md5sum of /usr/bin/last                      |
|  64232 | Md5sum of /usr/bin/ls                        |
|  64230 | Md5sum of /usr/bin/netstat                   |
|  65777 | Md5sum of /usr/bin/passwd                    |
|  64233 | Md5sum of /usr/bin/pstree                    |
|  64229 | Md5sum of /usr/bin/ps                        |
|  64235 | Md5sum of /usr/bin/sudo                      |
|  64231 | Md5sum of /usr/bin/top                       |
|  64228 | Md5sum of /usr/sbin/lsof                     |
|  64237 | Md5sum of /var/spool/cron/root               |
|  64238 | Md5sum of /var/spool/cron/yeemiao            |
|  62941 | Md5sum of {#SENSITIVEFILE}                   |
|  62940 | Zabbix Sensitive File Data Send Status       |
|  70637 | TCP Connect  Status Sender Data Status       |
|  70638 | TCP Connect Status CLOSED                    |
|  70639 | TCP Connect Status CLOSE_WAIT                |
|  70640 | TCP Connect Status CLOSING                   |
|  70641 | TCP Connect Status ESTABLISHED               |
|  70642 | TCP Connect Status FIN_WAIT1                 |
|  70643 | TCP Connect Status FIN_WAIT2                 |
|  70644 | TCP Connect Status LAST_ACK                  |
|  70645 | TCP Connect Status LISTEN                    |
|  70646 | TCP Connect Status SYN_RECV                  |
|  70647 | TCP Connect Status  SYN_SENT                 |
|  70648 | TCP Connect Status TIME_WAIT                 |
|  81714 | TCP Segments Retransmit 传出数               |
|  81518 | TCP Segments Retransmit 重传数               |
|  72398 | TCP Segments Retransmit Percent              |
|  67026 | IO_Stats rkB/s on vda                        |
|  67028 | IO_Stats util on vda                         |
|  67030 | IO_Stats wkB/s on vda                        |
|  67027 | IO_Stats rkB/s on vdb                        |
|  67029 | IO_Stats util on vdb                         |
|  67031 | IO_Stats wkB/s on vdb                        |
| 164935 | IO_Stats rkB/s on vdc                        |
| 164936 | IO_Stats util on vdc                         |
| 164937 | IO_Stats wkB/s on vdc                        |
|  66284 | IO_Stats rkB/s on {#DISK_NAME}               |
|  66285 | IO_Stats util on {#DISK_NAME}                |
|  66286 | IO_Stats wkB/s on {#DISK_NAME}               |
|  66282 | Linux DiskIO discovery                       |
| 381436 | es数据目录磁盘使用率                         |
| 390460 | es当前执行任务数                             |
|  49165 | Maximum number of opened files               |
|  49166 | Maximum number of processes                  |
|  67630 | Linux NetWork Devices Discovery              |
|  49167 | Network interface discovery                  |
|  49364 | Incoming network traffic on $1               |
|  49168 | Incoming network traffic on $1               |
|  49365 | Outgoing network traffic on $1               |
|  49169 | Outgoing network traffic on $1               |
|  49170 | Number of running processes                  |
|  49171 | Number of processes                          |
|  62939 | Linux Sensitive File Discovery               |
|  49172 | Host boot time                               |
|  49173 | Interrupts per second                        |
|  49174 | Processor load (15 min average per core)     |
|  49175 | Processor load (1 min average per core)      |
|  49176 | Processor load (5 min average per core)      |
|  49177 | Context switches per second                  |
|  49178 | CPU $2 time                                  |
|  49179 | CPU $2 time                                  |
|  49180 | CPU $2 time                                  |
|  49181 | CPU $2 time                                  |
|  49182 | CPU $2 time                                  |
|  49183 | CPU $2 time                                  |
|  49184 | CPU $2 time                                  |
|  49185 | CPU $2 time                                  |
|  49186 | Host name                                    |
|  49187 | Host local time                              |
|  49191 | System information                           |
|  49192 | System uptime                                |
|  49193 | Number of logged in users                    |
| 190188 | /etc/audit/audit.rules                       |
| 189723 | /etc/audit/auditd.conf                       |
| 189724 | /etc/hosts.allow                             |
| 189725 | /etc/hosts.deny                              |
|  49194 | Checksum of $1                               |
| 189726 | /etc/rsyslog.conf                            |
| 189727 | /etc/selinux/config                          |
|  49195 | Checksum of $1                               |
|  49196 | Mounted filesystem discovery                 |
|  49366 | Free inodes on $1 (percentage)               |
|  49367 | Free inodes on $1 (percentage)               |
|  49197 | Free inodes on $1 (percentage)               |
|  49368 | Free disk space on $1                        |
|  49370 | Free disk space on $1 (percentage)           |
|  49372 | Total disk space on $1                       |
|  49374 | Used disk space on $1                        |
|  49369 | Free disk space on $1                        |
|  49371 | Free disk space on $1 (percentage)           |
|  49373 | Total disk space on $1                       |
|  49375 | Used disk space on $1                        |
|  49198 | Free disk space on $1                        |
|  49199 | Free disk space on $1 (percentage)           |
|  49200 | Total disk space on $1                       |
|  49201 | Used disk space on $1                        |
|  49202 | 可用内存量                                   |
|  49203 | Total memory                                 |
|  49204 | 内存使用率                                   |
+--------+----------------------------------------------+
112 rows in set (0.00 sec)

 

3.获取监控项脚本

[root@hxl-elasticsearch-prd scripts]# more incoming_outgoing_network_traffic.sh 
#!/bin/bash

zabbix_host=192.168.1.100
user=hxl
passwd=123456

incoming_network_traffic=`mysql -h ${zabbix_host} -u${user} -p${passwd} -N<<EOF
use zabbix;
select 
value
from history_uint a
inner join items b
  on a.itemid = b.itemid
inner join hosts c
  on b.hostid = c.hostid
where c.host='hxl-elasticsearch-prd'
  and b.itemid in (49364)
order by clock desc limit 1;
EOF`


outgoing_network_traffic=`mysql -h ${zabbix_host} -u${user} -p${passwd} -N<<EOF
use zabbix;
select 
value
from history_uint a
inner join items b
  on a.itemid = b.itemid
inner join hosts c
  on b.hostid = c.hostid
where c.host='hxl-elasticsearch-prd'
  and b.itemid in (49365)
order by clock desc limit 1;
EOF`

##echo $incoming_network_traffic
##echo $outgoing_network_traffic


total_network_traffic=$(expr $incoming_network_traffic + $outgoing_network_traffic)


total_network_traffic_by_mb=$(echo "scale=1;$total_network_traffic/1024/1024" | bc)

##total_network_traffic_by_mb=20.1

echo $total_network_traffic_by_mb

 

posted @ 2025-06-10 10:24  slnngk  阅读(50)  评论(0)    收藏  举报