Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Zabbix Custom script

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

The idea of this script is to customize a key, and then calculate the real-time Nic traffic by looking at the values in the file / proc/net/dev. In fact, I am calculating an average. It is divided into in and out. If the script is executed every 1 minute, the calculated traffic value is the average of 1 minute. Finally, the network card traffic trend chart is obtained.

Client operation

# vim / etc/zabbix/zabbix_agentd.conf

# UnsafeUserParameters=0 uncomment and change it to 1, adding a line below

UserParameter=my.net.if [*], / usr/local/sbin/zabbix/net.sh $1 $2

My.net.if [equivalent to command name, he is key

UserParameter uses custom key values (similar to net.if.in). Scripts written by oneself often have parameters. [*] means that he has his own parameters, so he needs to add [*]. This is a fixed way of writing. If the script does not have any parameters at all, then this will be saved. After the comma is the path of the script we wrote, and then there are the parameters to be used, just write a few. If there are no parameters, the following $1 $2 and the preceding [*] can be omitted.

Inadequacies:

The script needs to be constantly improved in its work.

There is no separate file for each network card. All network cards use the same log file.

# vim / usr/local/sbin/zabbix/net.sh

#! / bin/bash

Eth=$1

Io=$2

Net_file= "/ proc/net/dev"

If [$2 = = "in"]

Then

New = `grep "$eth" $net_file | awk'{print $2}'`

Tmp/ neti.log`

N = `echo "$n_new-$n_old" | bc`

New = `date +% s`

Dailold = `tail-2 / tmp/neti.log | head-1`

D = `echo "$d_new-$d_old" | bc`

If_net= `echo "$nhammerd" | bc`

Echo $if_net

Date +% s > > / tmp/neti.log

Grep "$eth" $net_file | awk'{print $2}'> > / tmp/neti.log

Elif [$2 = = "out"]

Then

New = `grep "$eth" $net_file | awk'{print $10}'`

N tmp/ old = `tail-1 / neto.log`

N = `echo "$n_new-$n_old" | bc`

New = `date +% s`

Dailold = `tail-2 / tmp/neto.log | head-1`

D = `echo "$d_new-$d_old" | bc`

If_net= `echo "$nhammerd" | bc`

Echo $if_net

Date +% s > > / tmp/neto.log

Grep "$eth" $net_file | awk'{print $10}'> > / tmp/neto.log

Else

Echo 0

Fi

-

Detailed explanation

Eth=$1 # the first parameter, the name of the network card, the script is not specific to the network card, you can monitor many network cards, monitor in and out

The second parameter of io=$2 # is not io, but an abbreviation for in or out

Net_file= "/ proc/net/dev" # is the most critical file from which network card traffic is obtained. The idea of this script is to calculate the real-time Nic traffic by looking at the values in the file / proc/net/dev. In fact, I am calculating an average. It is divided into in and out. If the script is executed every 1 minute, the calculated traffic value is the average of 1 minute.

New = `grep "$eth" $net_file | awk'{print $2}'`# get the latest value from the above file

Nfolold = `tail-1 / tmp/ neti.log` # check the traffic of the last network card

N = `echo "$n_new-$n_old" | bc` # n is the difference between the two (the traffic of the above two new and old NICs is subtracted)

Dstamps new = `date +% s` # record the current timestamp

Head-1` # find the time stamp of the last time

D = `echo "$d_new-$d_old" | bc` # d is two time intervals

If_net= `echo "$nplink" | Bc` # to calculate the average value

Echo $if_net # output average

Date +% s > > / tmp/neti.log # the current timestamp is entered into the log

Grep "$eth" $net_file | awk'{print $2}'> > / tmp/neti.log # current traffic is entered into the log

-

The value is subtracted from the previous one, and then divided to find an average.

# chmod adepx / usr/local/sbin/zabbix/net.sh # change permissions

To execute the operation, first create the directories and files to be used in the script

# touch / tmp/ net[io] .log

# date +% s > > / tmp/neti.log

# grep eth0 / proc/net/dev | awk'{print $2}'> > / tmp/neti.log

# date +% s > > / tmp/neto.log

# grep eth0 / proc/net/dev | awk'{print $10}'> > / tmp/neto.log

# chown zabbix / tmp/ net[io] .log

Perform script detection:

# / usr/local/sbin/zabbix/net.sh eth0 in

# / usr/local/sbin/zabbix/net.sh eth0 out

At this time, an error will be reported when detecting the traffic of eth2 or other network cards, because before executing the script, grep filters out the traffic of eth0. The number obtained by using eth2 directly here must be incorrect. Therefore, if you want to detect eth2 traffic, you should use eth2 when grep and touch.

# / etc/init.d/zabbix-agent restart

Server side

# zabbix_get-s 192.168.32.150-p10050-k "my.net.if [eth0,out]" to view out Nic traffic in bytes

three hundred and ninety five

# zabbix_get-s 192.168.32.150-p10050-k "my.net.if [eth0,in]" to view in Nic traffic in bytes

three hundred and eight

WEB end

The key shown below can only be handwritten, not selectable.

Create eth0_net_in and eth0_net_out

Check to see if the addition is successful

Demand: count the number of people online per minute, and get the number of people online through the database.

Train of thought:

1. Add key to the client

UserParameter=my.online,/usr/local/sbin/zabbix/online.sh

# since this requirement has no parameters, the parameter option is removed

2. Write a script

Get the number of online people per minute from the database

3. Testing

The server performs the operation.

# zabbix_get-s 192.168.32.150-p10050-k "my.online"

If a parameter is returned, it represents success.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report