bash script to monitor packets

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Developers, Moderators

Post Reply
anone
Posts: 3
Joined: Tue Jan 09, 2024 10:00 am

bash script to monitor packets

Post by anone »

Hello,

I hope my question is in the right category.

I'm a student and our teacher has asked us to monitor packets sent on a machine with cacti. We have to do it via SNMP and via script. I did the part with SNMP and it's working fine, but I can't seem to make the script part work.

I've simplified my script to a maximum just to make sure I understand what's going on/ what I need to give to cacti.

Code: Select all

#!/bin/bash

# Function to get the number of received packets

get_received_packets() {

    netstat -i | grep enp0s3 | sed "s/ . */ /g" | cut -d" " -f3

}

# Infinite loop

while true; do

    # Get the number of received packets

    nbp=$(get_received_packets)

    # Output a timestamp and the number of received packets

    echo $nbp

    # Sleep for 5 seconds before the next iteration

    sleep 5

done
Data Input Method
data_input_method.PNG
data_input_method.PNG (27.82 KiB) Viewed 712 times
output_fields(DTI).PNG
output_fields(DTI).PNG (13.7 KiB) Viewed 712 times
Data source Template
data-source(tmp).PNG
data-source(tmp).PNG (51.12 KiB) Viewed 712 times
Graph template
graph_template.PNG
graph_template.PNG (73.83 KiB) Viewed 712 times
The rest is default settings/options.
data_output_graph.PNG
data_output_graph.PNG (35.15 KiB) Viewed 712 times
Add the graph on localhost device
Can't seem to add another screeshot but I went into Management>Devices>Local Linux Machine>*Create Graphs for this Device> and added "AAAA"

When I go to graphs, I see my graph but it's empty.

I have 751 permission and www-data:www-data on the script.

I'm sorry if I made a dumb mistake or forgot something, i've just discovered cacti about a week ago.

Thanks in advance.

Friendly yours, Anone
User avatar
macan
Cacti Guru User
Posts: 1137
Joined: Tue Mar 18, 2008 2:30 am
Location: Czech

Re: bash script to monitor packets

Post by macan »

You don't need infinite loop in script. You need script which only returns data and ends.

Have a look to cacti_installation_dir/scripts/unixprocesses.pl
Let the Cacti grow!
anone
Posts: 3
Joined: Tue Jan 09, 2024 10:00 am

Re: bash script to monitor packets

Post by anone »

Hello, thanks for answering!

I have modified the script as follow :

Code: Select all

#!/bin/bash

# Function to get the number of received packets

get_received_packets() {

    netstat -i | grep enp0s3 | sed "s/ . */ /g" | cut -d" " -f3

}
nbp=$(get_received_packets)

# Output 
echo $nbp
My graph is still empty tho...
empty.PNG
empty.PNG (193.66 KiB) Viewed 699 times
I must be missing something but I don't know what :)
User avatar
macan
Cacti Guru User
Posts: 1137
Joined: Tue Mar 18, 2008 2:30 am
Location: Czech

Re: bash script to monitor packets

Post by macan »

again - try to inspire from unix_processes. Maybe you need change data collection -> data input method or Templates -> data source or graph template
Let the Cacti grow!
anone
Posts: 3
Joined: Tue Jan 09, 2024 10:00 am

Re: bash script to monitor packets

Post by anone »

heya,

so I copied unix_process to have :

Code: Select all

#!/usr/bin/env perl

delete @ENV{qw(PATH)};
$ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin';

open(PROCESS, "netstat -i | grep enp0s3 | sed 's/ . */ /g' | cut -d' ' -f3 |");
$output = <PROCESS>;
close(PROCESS);
chomp($output);
print $output;

It seems to be working now ! The only thing that I dont like yet is the graph's numbers/units.
If I run the script manually I get about 2000 packets
result.PNG
result.PNG (25.55 KiB) Viewed 634 times
but on the graph the measurements isnt really accurate i feel.
graph.PNG
graph.PNG (71.54 KiB) Viewed 634 times
If you have an idea that'd be awesome, otherwise thanks a lot already !
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests