Cacti result from bash script is invalid or 0

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

Moderators: Developers, Moderators

Post Reply
burek123
Posts: 6
Joined: Sun May 06, 2018 4:12 pm

Cacti result from bash script is invalid or 0

Post by burek123 »

TL;DR:
  • Partial result U from bash script that works fine in a terminal ran as root or cacti user
  • Cacti is running as www-data, bash script file ownership and permissions are correct
  • Data sources and inputs are defined via GUI
  • Tried to rebuild poller cache, remove and create graphs
  • Tried different variations of bash scripting
  • Results still Invalid
I'm struggling with this problem for a while now and can't find the solution. I've searched all the posts about the same errors but I couldn't solve it.

I've made a custom bash script that connects to a printer via SNMP and fetches toner levels data. When I run the script in a terminal, I get the correct result but cacti can't handle it.

This is output from the cacti logfile:
2018/05/08 21:45:03 - POLLER: Poller[Main Poller] WARNING: Invalid Response(s), Errors[1] Device[10] Device[Xerox Phaser 3020] Thread[1] Graphs[Toner percentage, Toner percentage] DS[Get printer toner level]
2018/05/08 21:45:03 - POLLER: Poller[Main Poller] Device[10] Device[Xerox Phaser 3020] Graphs[Toner percentage, Toner percentage] DS[Get printer toner level] CMD: /var/www/html/scripts/xerox_black_toner_level.sh '' '', output: U
2018/05/08 21:45:03 - POLLER: Poller[Main Poller] Device[10] Device[Xerox Phaser 3020] Graphs[Toner percentage, Toner percentage] DS[Get printer toner level] WARNING: Result from CMD not valid. Partial Result: U

Cacti is running as www-data, file is executable and owned by www-data.

File permissions output:
-rwxr-xr-x 1 www-data www-data 793 May 8 21:41 xerox_black_toner_level.sh*

This is the bash script:

Code: Select all

#!/bin/sh

# Tested on Xerox Phaser 3020

# Usage: <path_cacti>/scripts/xerox_black_toner.sh <hostname> <snmp_community>

#Install bc and snmpget for this script to work
BC_PATH="/usr/bin/bc"

# Toner current level
tonerlevel=$(/usr/bin/snmpget -v2c -c $2 $1 1.3.6.1.2.1.43.11.1.1.9.1.1 | awk '{ printf($4) }')

# Toner max level
tonermaxcapacity=$(/usr/bin/snmpget -v2c -c $2 $1 1.3.6.1.2.1.43.11.1.1.8.1.1 | awk '{ printf($4) }')

# Toner left percentage
tonerleft=$(echo "scale=2;$tonerlevel / $tonermaxcapacity *100" | $BC_PATH | cut -f1 -d".")

printf $tonerleft
Bash script terminal output as root user:
root@logstation:/var/www/html/scripts# ./xerox_black_toner_level.sh 192.168.0.251 public
98root@logstation:/var/www/html/scripts#

Bash script terminal output as www-data user:
root@logstation:/var/www/html/scripts# sudo -u www-data sh -c "/var/www/html/scripts/xerox_black_toner_level.sh 192.168.0.251 public"
98root@logstation:/var/www/html/scripts#

Data sources, data inputs and graph are created via GUI and are working if I put a direct value into script that is

Code: Select all

printf 98
instead of

Code: Select all

printf $tonerleft
If I set

Code: Select all

printf "%d" $tonerleft
cacti reports a value of 0 instead Invalid U.

I've made a simple graph without any templates based on this guide: https://www.cacti.net/downloads/docs/html/how_to.html

GUI settings:

Image
Image
Image

What have I tried already:
  • Tried different variations with bash script (echo instead of printf, inputting direct values etc.)
  • Rebuilt poller cache many times
  • Removed graphs, deleted data and .rrd files, started over
  • Went trough similar forum posts and applied their solutions, nothing worked
The only thing I can think of now is probably a bash script coding error, but it does work from a terminal. Any clues?
netniV
Cacti Guru User
Posts: 3441
Joined: Sun Aug 27, 2017 12:05 am

Re: Cacti result from bash script is invalid or 0

Post by netniV »

OK you seem to be confusing things here. You say this is a bash script, yet you are using the standard posix SH as the script handler.

Surely the shell command should be:

#!/bin/bash
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
tertius
Cacti User
Posts: 82
Joined: Wed Mar 01, 2017 2:34 pm

Re: Cacti result from bash script is invalid or 0

Post by tertius »

You seem to make it overly complicated. You get 2 values via script from snmp, compute a ratio and use the external scripting facility of cacti to get the result into cacti.

But cacti is able to query snmp directly, and it is able to do computations with queried values.

Create a single data template that queries your 2 snmp values in 2 data source items.
Create a cdef that performs the computation of the ratio of data source a and b. It's something like a,b,/
Create a graph template that uses the data template.
Add a graph item and configure the cdef for this item.

This is a solution that can be clicked within the cacti gui, no external scripts required.
burek123
Posts: 6
Joined: Sun May 06, 2018 4:12 pm

Re: Cacti result from bash script is invalid or 0

Post by burek123 »

netniV wrote:OK you seem to be confusing things here. You say this is a bash script, yet you are using the standard posix SH as the script handler.

Surely the shell command should be:

#!/bin/bash
Yes, I've tried both, sh and bash, same results.
burek123
Posts: 6
Joined: Sun May 06, 2018 4:12 pm

Re: Cacti result from bash script is invalid or 0

Post by burek123 »

tertius wrote:You seem to make it overly complicated. You get 2 values via script from snmp, compute a ratio and use the external scripting facility of cacti to get the result into cacti.

But cacti is able to query snmp directly, and it is able to do computations with queried values.

Create a single data template that queries your 2 snmp values in 2 data source items.
Create a cdef that performs the computation of the ratio of data source a and b. It's something like a,b,/
Create a graph template that uses the data template.
Add a graph item and configure the cdef for this item.

This is a solution that can be clicked within the cacti gui, no external scripts required.
Yeah, I like your solution but I still want to figure out what is the problem. In the future I will probably need to fetch some data from non SNMP sources and your solution will not work, but I will definitely try it for this exact case.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests