Unable to graph my own bash script using cacti

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Post Reply
khalilmeb
Posts: 12
Joined: Thu Jun 20, 2019 9:20 am

Unable to graph my own bash script using cacti

Post by khalilmeb »

Hello :)

I have the following output of running SNMPstat which is

Code: Select all

 snmpnetstat -v2c -c public -Cs -Cp udp 172.0.1.11 
The output of the command is:

Code: Select all

udp:
           198 total datagrams received
            65 datagrams to invalid port
             0 datagrams dropped due to errors
           265 output datagram requests
I know that the Data input methods with more than one output field are handled a bit differently when writing scripts. Scripts that output more than one value should be formatted like the following:

Code: Select all

<fieldname_1>:<value_1> <fieldname_2>:<value_2> ... <fieldname_n>:<value_n>

Therefore, I have written the *bash script* and it returns exactly the output I want when I run for example `

Code: Select all

./udp_netstat.sh 172.0.1.11
` returns

Code: Select all

Received Datagrams:258 Invalid port:63 Dropped datagrams:0 Datagram requests:471520
Here is the content of the bash file

Code: Select all

$ cat udp_netstat.sh
    #!/bin/bash
    
    rs="$(snmpnetstat -v2c -c public -Cs -Cp udp "$@")"
        # You have to quote "$rs" so newlines don't break
        ReceivedDatagrams="$(echo "$rs" | cut -d$'\n' -f2 | tr -s ' '| cut -d' ' -f2)"
        InvalidPort="$(echo "$rs" | cut -d$'\n' -f3 | tr -s ' '| cut -d' ' -f2)"
        DroppedDatagrams="$(echo "$rs" | cut -d$'\n' -f4 | tr -s ' '| cut -d' ' -f2)"
        DatagramRequests="$(echo "$rs" | cut -d$'\n' -f5 | tr -s ' '| cut -d' ' -f2)"
the value `"$@"` is for the hostname device for cacti

Starting with a data input method, I gave the following path `/usr/share/cacti/site/scripts/udp_netstat <hostname>` then I added the input fields as well as the output, according to the bash file output.

For the Data Source, I added data source items also according to the bash file output and made Data source type as COUNTER. Then, I proceed to Graph Template where I added the line of each output I have.

Here is the steps and some screenshots:

Image

It seems to be in the workflow of adding a new script, however, nothing so far is shown in the graph.

Input Data Source
Image

Data Template
Image

Graph Template
Image

I assume there is a mistake with the Input String.


Thank you :)
Last edited by khalilmeb on Wed Jul 03, 2019 11:25 am, edited 4 times in total.
netniV
Cacti Guru User
Posts: 3441
Joined: Sun Aug 27, 2017 12:05 am

Re: How do I make my own Scripts Work With Cacti?

Post by netniV »

You'll find examples in the cacti scripts folder already. That's where I learnt how to do it.

I have collaborated on scripts with various companies though I usually charge for that time because when writing a custom script, its also the maintenance and support of it when they want more or something stops working (like the output from the sub process changes).

I think Thomas Urban's cacti book also documents in some basic ways how to handle this, but I couldn't be sure without digging out a copy.
Cacti Developer & Release Manager
The Cacti Group

Director
BV IT Solutions Ltd

+--------------------------------------------------------------------------+

Cacti Resources:
Cacti Website (including releases)
Cacti Issues
Cacti Development Releases
Cacti Development Documentation
khalilmeb
Posts: 12
Joined: Thu Jun 20, 2019 9:20 am

Re: How do I make my own Scripts Work With Cacti?

Post by khalilmeb »

netniV wrote:You'll find examples in the cacti scripts folder already. That's where I learnt how to do it.

I have collaborated on scripts with various companies though I usually charge for that time because when writing a custom script, its also the maintenance and support of it when they want more or something stops working (like the output from the sub process changes).

I think Thomas Urban's cacti book also documents in some basic ways how to handle this, but I couldn't be sure without digging out a copy.

I have edit my post. Could you please check it?
netniV
Cacti Guru User
Posts: 3441
Joined: Sun Aug 27, 2017 12:05 am

Re: Unable to graph my own bash script using cacti

Post by netniV »

Try changing your field names so that they don't have spaces.
Cacti Developer & Release Manager
The Cacti Group

Director
BV IT Solutions Ltd

+--------------------------------------------------------------------------+

Cacti Resources:
Cacti Website (including releases)
Cacti Issues
Cacti Development Releases
Cacti Development Documentation
khalilmeb
Posts: 12
Joined: Thu Jun 20, 2019 9:20 am

Re: Unable to graph my own bash script using cacti

Post by khalilmeb »

netniV wrote:Try changing your field names so that they don't have spaces.
I think I have given the correct names where don't have any spaces. Except for the friendly names, don't you think so?

Image
Image
Image
netniV
Cacti Guru User
Posts: 3441
Joined: Sun Aug 27, 2017 12:05 am

Re: Unable to graph my own bash script using cacti

Post by netniV »

Your screenshot of the script output does not match up with the names on the data query from what I saw ...
Cacti Developer & Release Manager
The Cacti Group

Director
BV IT Solutions Ltd

+--------------------------------------------------------------------------+

Cacti Resources:
Cacti Website (including releases)
Cacti Issues
Cacti Development Releases
Cacti Development Documentation
khalilmeb
Posts: 12
Joined: Thu Jun 20, 2019 9:20 am

Re: Unable to graph my own bash script using cacti

Post by khalilmeb »

netniV wrote:Your screenshot of the script output does not match up with the names on the data query from what I saw ...
Well, this is the script I use:

Code: Select all

#!/bin/bash
rs="$(snmpnetstat -v2c -c public -Cs -Cp udp "$@")"
ReceivedDatagrams=$(echo $rs | cut -d"/" -f1)
InvalidPort=$(echo $rs | cut -d"/" -f2)
DroppedDatagrams=$(echo $rs | cut -d"/" -f3)
DatagramRequests=$(echo $rs | cut -d"/" -f4)
    
echo "Received Datagrams:$ReceivedDatagrams Invalid port:$InvalidPort Dropped datagrams:$DroppedDatagrams Datagram requests:$DatagramRequests"
the value "$@" is for the hostname device for cacti

The output of bash script is:

Code: Select all

ReceivedDatagrams:258 InvalidPort:63 DroppedDatagrams:0 DatagramRequests:471520
I don't see why It is not working
netniV
Cacti Guru User
Posts: 3441
Joined: Sun Aug 27, 2017 12:05 am

Re: Unable to graph my own bash script using cacti

Post by netniV »

Well, your post does not add up. You have spaces in your echo statement that are not in your output.
Cacti Developer & Release Manager
The Cacti Group

Director
BV IT Solutions Ltd

+--------------------------------------------------------------------------+

Cacti Resources:
Cacti Website (including releases)
Cacti Issues
Cacti Development Releases
Cacti Development Documentation
khalilmeb
Posts: 12
Joined: Thu Jun 20, 2019 9:20 am

Re: Unable to graph my own bash script using cacti

Post by khalilmeb »

netniV wrote:Well, your post does not add up. You have spaces in your echo statement that are not in your output.
How to fix this issue? I actually don't get it how I fix the space thing. Could you please write me down the correct echo?
User avatar
camerabob
Cacti User
Posts: 386
Joined: Fri Feb 10, 2017 2:45 pm
Location: Long Island, New York, USA
Contact:

Re: Unable to graph my own bash script using cacti

Post by camerabob »

Change:

Code: Select all

echo "Received Datagrams:$ReceivedDatagrams Invalid port:$InvalidPort Dropped datagrams:$DroppedDatagrams Datagram requests:$DatagramRequests"
to

Code: Select all

echo "Received_Datagrams:$ReceivedDatagrams_Invalid port:$InvalidPort Dropped_datagrams:$DroppedDatagrams Datagram_requests:$DatagramRequests"
The output should now present as:

Code: Select all

Received_Datagrams:258 Invalid_Port:63 Dropped_Datagrams:0 Datagram_Requests:471520
Make the same " " to underscore changes in Cacti and the data should sync. Or you could just remove those spaces altogether.
Prod: Cacti 1.2.15 @ CentOS Linux release 7.9.2009 (Core) & PHP 5.4.16-48.el7
Maint @ 1.2
Monitor @ 2.3.6
Thold @ 1.2.4

Temp: Cacti 1.2.3 @ CentOS Linux release 7.9.2009 (Core) & PHP 5.4.16-48.el7
Flowview @ 2.1
Mactrack @ 4.2
Maint @ 1.2
Monitor @ 2.3.6
Router Configs @ 1.3.4
Syslog Monitoring @ 2.1
Thold @ 1.2.4
netniV
Cacti Guru User
Posts: 3441
Joined: Sun Aug 27, 2017 12:05 am

Re: Unable to graph my own bash script using cacti

Post by netniV »

Thanks Bob for the examples. Personally, I would use camel case with only alphanumeric characters to avoid any issues. There should only be a space between the value and the next fieldname.
Cacti Developer & Release Manager
The Cacti Group

Director
BV IT Solutions Ltd

+--------------------------------------------------------------------------+

Cacti Resources:
Cacti Website (including releases)
Cacti Issues
Cacti Development Releases
Cacti Development Documentation
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest