Script result failing to graph

Post support questions that directly relate to Linux/Unix operating systems.

Moderators: Developers, Moderators

liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Script result failing to graph

Post by liviusbr »

Hi,

I am trying to graph the content of a python script which reads the temperature from a sensor.
I am calling the script from the following path :

python /usr/share/cacti/site/scripts/AdafruitDHumid.py 2302 4

, where 2302 and 4 are the sensor type and pin numbers.

Basically I am getting the following error:

09/20/2016 12:25:05 PM - CMDPHP: Poller[0] Host[1] DS[17] WARNING: Result from CMD not valid. Partial Result: U


when I execute the script as root I get the correct result (measurement value).

The script content is :


import sys

import Adafruit_DHT


# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
sys.exit(1)

# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None:
print '{1:0.1f}'.format(temperature, humidity)
else:
print 'Failed to get reading. Try again!'
sys.exit(1)
cigamit
Developer
Posts: 3367
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Script result failing to graph

Post by cigamit »

You fail to mention what user the poller is running as, is it root also? You may also need to supply the full path to python.
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

I have not changed anything in the poller settings, so I assume it is running as cacti user?

crontab -l , running as root, shows the following

*/5 * * * * cactiuser php /usr/share/cacti/site/poller.php > /dev/null 2>&1

I added this line manually.

What else am I missing?
cigamit
Developer
Posts: 3367
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Script result failing to graph

Post by cigamit »

What OS are you running?

Most OSes, when you run crontab as root, show the roots crontab. Which means that cactiuser shouldn't be there as you shouldn't be specifying any username (and really I am even surprised the command works at all as it will try to execute that as a command).

Did you actually create a cactiuser? As root, can you run
crontab -l -u cactiuser
and get anything back?
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

cigamit wrote:What OS are you running?

Most OSes, when you run crontab as root, show the roots crontab. Which means that cactiuser shouldn't be there as you shouldn't be specifying any username (and really I am even surprised the command works at all as it will try to execute that as a command).

Did you actually create a cactiuser? As root, can you run
crontab -l -u cactiuser
and get anything back?

Hi,

I am using a raspberryPi.
I created cactiuser, w/o a password.


root@raspberrypi:/usr/sbin# crontab -l -u cactiuser
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * cactiuser php /usr/share/cacti/site/poller.php > /dev/null 2>&1
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

liviusbr wrote:
cigamit wrote:What OS are you running?

Most OSes, when you run crontab as root, show the roots crontab. Which means that cactiuser shouldn't be there as you shouldn't be specifying any username (and really I am even surprised the command works at all as it will try to execute that as a command).

Did you actually create a cactiuser? As root, can you run
crontab -l -u cactiuser
and get anything back?

Hi,

I am using a raspberryPi.
I created cactiuser, w/o a password.


root@raspberrypi:/usr/sbin# crontab -l -u cactiuser
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * cactiuser php /usr/share/cacti/site/poller.php > /dev/null 2>&1

..And that's the crontab log:

Sep 20 17:09:01 raspberrypi CRON[29112]: (root) CMD ( [ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean)
Sep 20 17:10:01 raspberrypi CRON[29196]: (root) CMD (cactiuser php /usr/share/cacti/site/poller.php > /dev/null 2>&1)
Sep 20 17:10:01 raspberrypi CRON[29197]: (www-data) CMD (php --define suhosin.memory_limit=512M /usr/share/cacti/site/poller.php 2>&1 >/dev/null | if [ -f /usr/bin/ts ] ; then ts ; else tee ; fi >> /var/log/cacti/poller-error.log)
Sep 20 17:15:01 raspberrypi CRON[29412]: (www-data) CMD (php --define suhosin.memory_limit=512M /usr/share/cacti/site/poller.php 2>&1 >/dev/null | if [ -f /usr/bin/ts ] ; then ts ; else tee ; fi >> /var/log/cacti/poller-error.log)
Sep 20 17:15:01 raspberrypi CRON[29417]: (cactiuser) CMD (cactiuser php /usr/share/cacti/site/poller.php > /dev/null 2>&1)
Sep 20 17:15:01 raspberrypi CRON[29419]: (root) CMD (cactiuser php /usr/share/cacti/site/poller.php > /dev/null 2>&1)
cigamit
Developer
Posts: 3367
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Script result failing to graph

Post by cigamit »

Ya, it appears that you have 2 pollers running. Does your cacti log show 2 system stats entries every 5 minutes? We should probably get this cleared up before we move onto the script issue.
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

cigamit wrote:Ya, it appears that you have 2 pollers running. Does your cacti log show 2 system stats entries every 5 minutes? We should probably get this cleared up before we move onto the script issue.
You are right, for some reasons the crontab entry was present into root's crontab, and cactiuser's, therefore 2 system stats.
I cleared the crontab for root, now I should be find with cactiuser's crontab. I still dont get the graph,though...
cigamit
Developer
Posts: 3367
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Script result failing to graph

Post by cigamit »

Ok, the first things you will want to do is also remove the "cactiuser" from cactiusers's crontab, its shouldn't be there, that is only if you are editting one of the cron.d files that can have multiple users in it, so it needs to know what user to run it as.

Next you are going to want to verify that that python command can even be ran as the user cactiuser
So you will need to switch to the cactiuser account (su - cactiuser) and attempt to run the command. If it is accessing those pins, I am betting it is going to need root access to do it (the script even mentions running it via sudo).

If it does need root access, then I recommend you taking the poller out of cactiusers and sticking it back into roots cron. From there, you will need to edit the data input / data query, etc.. and update the command to have the full path to python in it. You can find it with "which python". We can then debug it further from there after the easy stuff is out of the way.
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

cigamit wrote:Ok, the first things you will want to do is also remove the "cactiuser" from cactiusers's crontab, its shouldn't be there, that is only if you are editting one of the cron.d files that can have multiple users in it, so it needs to know what user to run it as.

Next you are going to want to verify that that python command can even be ran as the user cactiuser
So you will need to switch to the cactiuser account (su - cactiuser) and attempt to run the command. If it is accessing those pins, I am betting it is going to need root access to do it (the script even mentions running it via sudo).

If it does need root access, then I recommend you taking the poller out of cactiusers and sticking it back into roots cron. From there, you will need to edit the data input / data query, etc.. and update the command to have the full path to python in it. You can find it with "which python". We can then debug it further from there after the easy stuff is out of the way.
Indeed, the cactiuser was unable to access those pins so I moved back to root's crontab, and deleted the cactiuser's crontab.
Now, root's crontab, looks like this :

*/5 * * * * php /usr/share/cacti/site/poller.php > /dev/null 2>&1

Then,
My data inputs is attached.
Still not getting the graph, yet. Running the script manually from the location is returning a result.
Attachments
Capture.PNG
Capture.PNG (22.39 KiB) Viewed 2244 times
cigamit
Developer
Posts: 3367
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Script result failing to graph

Post by cigamit »

Can you change the logging (under Settings) into Medium mode and after the poller runs again, find in the Cacti log where it is running the script and post that.
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

Hi cigamit,

I managed to fix it thanks to your hints. I have graphing now.
The problem is, I have a lot of gaps in between the graphs. How can I debug that?

Regards,
Liviu Sisu
cigamit
Developer
Posts: 3367
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Re: Script result failing to graph

Post by cigamit »

That can be any number of problems. It boils down to that its not writing the data to file. The reasoning can be multiple things, such as the device isn't responding in time, poller overrun, time syncing issues, etc... There should be some error in the Cacti log.

First thing to check is how long your poller is taking to run.
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

cigamit wrote:That can be any number of problems. It boils down to that its not writing the data to file. The reasoning can be multiple things, such as the device isn't responding in time, poller overrun, time syncing issues, etc... There should be some error in the Cacti log.

First thing to check is how long your poller is taking to run.

Indeed, I am getting the following error:

09/27/2016 03:26:20 PM - CMDPHP: Poller[0] Host[1] DS[15] CMD: /usr/bin/python /usr/share/cacti/site/scripts/AdafruitDHumid.py 2302 4, output: Failed to get reading. Try again!

How can I measure the time it takes the poller to run?
liviusbr
Posts: 12
Joined: Tue Sep 20, 2016 4:41 am

Re: Script result failing to graph

Post by liviusbr »

..and also:

09/27/2016 03:15:02 PM - POLLER: Poller[0] NOTE: Cron is configured to run too often! The Poller Interval is '300' seconds, with a minimum Cron period of '300' seconds, but only 0 seconds have passed since the poller last ran.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests