/usr/local/bin/mail.php is only executed from the console



  • I am trying to set up email notifications for UPS events (running NUT). I am very close to a working configuration, but I can only get it to work from the console.

    If I call my script (upssched-cmd.sh) from the console I will get an email, but if upssched-cmd.sh is called by NUT nothing happens. I can see that NUT is correctly executing upssched-cmd.sh due to the wall events, but for some reason mail.php is not executed.

    As can be seen from my file below I have tried to capture any php errors, as well as have mail.php echo a success message. This works from the console, but again, nothing is happening if NUT initiates the script. I have also tried to specifically list the php paths, but this has not changed anything.

    I believe this could be a permission issue, but upsmon.conf is already configured to run as root (RUN_AS_USER root).

    #! /bin/sh
    
    printf=/usr/bin/printf
    grep=/usr/bin/grep
    tail=/usr/bin/tail
    
    current_day=`date +%m/%d/%y`
    current_time=`date +%H:%M:%S`
    DATUM="$current_day @ $current_time"
    
    HOSTNAME=/bin/hostname
    
    UPSLOG=`cat /var/msgs | $grep ups | $tail -50`
    
    case $NOTIFYTYPE in
    	ONBATT)
    		UPS_STATUS = `upsc UPS@localhost ups.status`
    		UPS_BATT=`upsc UPS@localhost battery.charge`
    		UPS_RUNTIME=`upsc UPS@localhost battery.runtime`
    		UPS_LOAD=`upsc UPS@localhost ups.load`
    
    		EMAIL_SUBJECT = "Battery mode!"
    		EMAIL_CONTENT = "The UPS has kicked in and services are now running on battery!\n\nUPS Status: $UPS_STATUS\nUPS Battery Charge: $UPS_BATT%\nUPS Runtime: $UPS_RUNTIME seconds\n\n Load: $UPS_LOAD%\n\nNUT Log:\n$UPSLOG"
    		;;
    	ONLINE)
    		UPS_STATUS = `upsc UPS@localhost ups.status`
    		UPS_BATT=`upsc UPS@localhost battery.charge`
    		UPS_RUNTIME=`upsc UPS@localhost battery.runtime`
    		UPS_LOAD=`upsc UPS@localhost ups.load`
    
    		EMAIL_SUBJECT = "On line mode"
    		EMAIL_CONTENT = "The Server UPS is back on Line Power now.\n\n\nUPS Status: $UPS_STATUS\nUPS Battery Charge: $UPS_BATT%\nUPS Runtime: $UPS_RUNTIME seconds\n\nNUT Log:\n$UPSLOG"
     		;;
    	COMMBAD)
    		EMAIL_SUBJECT = "Communication error"
    		EMAIL_CONTENT = "The server has not been able to reach the UPS in xx minutes.\n\nNUT Log:\n$UPSLOG"
    		;;
    	*)
    		EMAIL_CONTENT="An unknown event was not picked up ($1). Check /usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh"
    		EMAIL_SUBJECT="unknown"
    		echo "wrong parameter ($NOTIFYTYPE)"
    		;;
    esac
    
    printf "$DATUM\n$*\n\n$EMAIL_CONTENT" | /usr/local/bin/php -q -c"/usr/local/etc/php.ini" /usr/local/bin/mail.php -s"UPS Alert: $EMAIL_SUBJECT" >> /usr/pbi/nut-amd64/local/etc/nut/err.log
    
    printf "$UPSNAME notification email sent" | /usr/bin/wall 
    
    exit 0
    


  • Just to clarify: NUT is calling the script via the NOTIFYCMD variable in upsmon.conf

    NOTIFYCMD "/usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh"
    

    My grief is all caused by the below single line in upssched-cmd.sh apparently not being executed:

    printf "$DATUM\n$*\n\n$EMAIL_CONTENT" | /usr/local/bin/php -q -c"/usr/local/etc/php.ini" /usr/local/bin/mail.php -s"UPS Alert: $EMAIL_SUBJECT"
    


  • @Lanes:

    My grief is all caused by the below single line in upssched-cmd.sh apparently not being executed:

    printf "$DATUM\n$*\n\n$EMAIL_CONTENT" | /usr/local/bin/php -q -c"/usr/local/etc/php.ini" /usr/local/bin/mail.php -s"UPS Alert: $EMAIL_SUBJECT"
    

    Good news …
    I executed this directly from the command line (SSH access):

    printf "07/08/2015\n\n\nMy Mail" | /usr/local/bin/php -q -c"/usr/local/etc/php.ini" /usr/local/bin/mail.php -s"UPS Alert: EMAIL_SUBJECT"
    

    and I received the mail.

    When I create the file " /usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh" (chowned it to uccp to be sure - and chmod +x also) and execute this file, I received this mail

    08/07/15 @ 19:21:42
    An unknown event was not picked up (). Check /usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh
    
    

    which seems correct to me.

    Btw:
    I presume that you have this on the Status: NUT Status => NUT Settings => Advanced features => upsmon.conf options (and click on Advanced) :

    NOTIFYFLAG ONLINE     SYSLOG+WALL+EXEC;
    NOTIFYFLAG ONBATT     SYSLOG+WALL+EXEC;
    NOTIFYFLAG LOWBATT    SYSLOG+WALL+EXEC;
    NOTIFYFLAG FSD        SYSLOG+WALL+EXEC;
    NOTIFYFLAG COMMOK     SYSLOG+WALL+EXEC;
    NOTIFYFLAG COMMBAD    SYSLOG+WALL+EXEC;
    NOTIFYFLAG SHUTDOWN   SYSLOG+WALL+EXEC;
    NOTIFYFLAG REPLBATT   SYSLOG+WALL+EXEC;
    NOTIFYFLAG NOCOMM     SYSLOG+WALL+EXEC;
    NOTIFYFLAG NOPARENT   SYSLOG+WALL+EXEC;
    NOTIFYCMD "/usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh";
    


  • Thanks, but I am getting the emails if I run the line directly as well, but not when it is being executed by NUT.

    This is my upsmon.conf file. Note that I edit the file directly, and not via the web interface. I know for a fact NUT correctly runs the file (I can add specific WALL messages that will be printed), but no luck getting it to send the email…

    RUN_AS_USER root
    
    MONITOR UPS@localhost 1 monuser *********** master
    MINSUPPLIES 1
    SHUTDOWNCMD "/sbin/shutdown -h +0"
    NOTIFYCMD "/usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh"
    POLLFREQ 2
    POLLFREQALERT 1
    HOSTSYNC 15
    DEADTIME 15
    POWERDOWNFLAG /etc/killpower
    
    NOTIFYMSG ONLINE	"UPS %s on line power"
    NOTIFYMSG ONBATT	"UPS %s on battery"
    NOTIFYMSG LOWBATT	"UPS %s battery is low"
    NOTIFYMSG FSD		"UPS %s: forced shutdown in progress"
    NOTIFYMSG COMMOK	"Communications with UPS %s established"
    NOTIFYMSG COMMBAD	"Communications with UPS %s lost"
    NOTIFYMSG SHUTDOWN	"Auto logout and shutdown proceeding"
    NOTIFYMSG REPLBATT	"UPS %s battery needs to be replaced"
    NOTIFYMSG NOCOMM	"UPS %s is unavailable"
    NOTIFYMSG NOPARENT	"upsmon parent process died - shutdown impossible"
    
    NOTIFYFLAG ONLINE       SYSLOG+WALL+EXEC
    NOTIFYFLAG ONBATT       SYSLOG+WALL+EXEC
    NOTIFYFLAG LOWBATT      SYSLOG+WALL+EXEC
    NOTIFYFLAG FSD          SYSLOG+WALL+EXEC
    NOTIFYFLAG COMMOK       SYSLOG+WALL+EXEC
    NOTIFYFLAG COMMBAD      SYSLOG+WALL+EXEC
    NOTIFYFLAG SHUTDOWN     SYSLOG+WALL+EXEC
    NOTIFYFLAG REPLBATT     SYSLOG+WALL+EXEC
    NOTIFYFLAG NOCOMM       SYSLOG+WALL+EXEC
    NOTIFYFLAG NOPARENT     SYSLOG+WALL+EXEC
    
    RBWARNTIME 43200
    
    NOCOMMWARNTIME 600
    
    FINALDELAY 5
    


  • This is what I get when checking permissions on the file. Could this be the issue?

    $ls -l /usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh
    -rwxr-xr-x 1 root wheel 1796 Aug 7 13:26



  • Well, all NUT config file are 'owned' bu 'uucp', but I guess it isn't needed.

    I can't do a real 'test' right now, I'm not on site ;)

    Sunday (tomorrow) I'm goint to test-drive it.

    Btw : read http://wiki.ipfire.org/en/addons/nut/start
    and look at the yellow warning:

    the '&' at the and of the sendmail command is mandatory upssched will only wait a few ms for the end of the script but sendmail may take up to 3-5 seconds to send the mail, so upssched will try to hit the trigger again and will also only wait a few ms till it get a return code so in the end it will send 3-5 emails instant of only one. With the & the sendmail command will be send to background an the script returns immediately the error 0 and all are happy :)

    Your not using 'sendmail', you start an instance of php (/usr/local/bin/php) using settings (/usr/local/etc/php.ini) to have /usr/local/bin/mail.php being executed.
    Add the & to that line to have it thrown in the background.

    First line:
    #!/bin/sh
    there was a space in it, remove it (?!)


  • Banned

    "-rwxr-xr-x" is executable for anyone, that's not the problem. But the shebang is indeed obviously wrong.



  • Fixed the shebang, and added & at the end of the email line - still does not run  ???



  • Hi,

    At this stage, I'm seeing the same results as you do.
    The script runs fine /usr/pbi/nut-amd64/local/etc/nut/upssched-cmd.sh from the command line … but .. no mails (edit: when NUT starts upssched-cmd.sh) .

    Btw: change the definition of "UPSLOG".
    It should read :

    UPSLOG=`clog /var/log/system.log | $grep ups | $tail -5`
    

    I'm still 'fighting' with

    printf "$DATUM\n$*\n\n$EMAIL_CONTENT" | /usr/local/bin/mail.php -s"UPS Alert: $EMAIL_SUBJECT" &
    

    Which is a condensed version - command line works, from script: No.
    It seems to me that /usr/local/bin/mail.php isn't even executed.

    Your questions were helpful to me : it seems that the battery of my UPS is in a bad shape. I have it changed it before I continu testing.



  • I've been trying this week to make this work.

    I used this script:

    #!/bin/sh
    
    WHOAMI=`whoami`
    printf "Script executed by $WHOAMI" | /usr/bin/wall 
    
    logger 1223
    /usr/local/bin/php -q /root/test.php
    logger 5678
    
    exit 0
    

    I know "uupc" (NUT) calls this script, because I found these in the main log:

    Aug 14 12:20:20 	root: 5678
    Aug 14 12:20:20 	root: 1223
    Aug 14 12:20:20 	upsmon[94982]: UPS APC-UPS@localhost on line power
    Aug 14 12:20:15 	root: 5678
    Aug 14 12:20:15 	root: 1223
    Aug 14 12:20:15 	upsmon[94982]: UPS APC-UPS@localhost on battery
    
    

    But : no trace of why this
    /usr/local/bin/php -q /root/test.php
    isn't executed

    Btw: /root/test.php

    #!/usr/local/bin/php -q
    require_once("util.inc");
    
    log_error("In here !!");
    
    ?>
    

    Btw : modo : this thread should be moved to the "Packages" forum.



  • Unfortunately I have not had any time to research any more into this myself for the past week, so thanks for your efforts. Do we believe this to be a bug, or just some misconfiguration of pfsense/nut?

    I have a few things I want to test when given the opportunity:

    1. Can the NOTIFYCMD script call / execute any non-php file
    2. Does the issue remain if we set NUT to use upssched, and then call the php script via the CMDSCRIPT variable instead
    3. Does the issue remain if we do write the commands inline in upsmon (NOTIFYCMD = 'do stuff here')


  • I got bored trying to find a solution for this issue back in August, and I have not looked at it until today. Since August there has been a few updates of the NUT package that I silently hoped would fix my issue, but no luck. I still can't get any php script to trigger from NUT, and I am at a loss where to look next.

    Where can I report this issue?


  • Rebel Alliance Developer Netgate

    have you tried:

    /usr/local/bin/php -f /usr/local/bin/mail.php
    

    Rather than executing it directly?



  • Tried that, and I have lost count for how many variations I have tried. It just won't execute the php. I am now using the same script as Gertjan posted above to test, and I have the exact same results as he.



  • I have the same issue.  I've never been able to get it working.  There's a related feature request here.



  • Good find ryanjaeb, I had not come across that for all my searching. I am a bit disappointed by the feature not being in place, but at least it appears the issue is not on my end.

    On a related topic: How do you guys then monitor the status of your UPS? I suppose I can create a separate log file, and possibly monitor that for changes to send email notifications. It would not be instant, but it could work.



  • Hi Everyone,

    I'm going absolutely mad myself trying to get email notification functionality to work via the NUT package and NOTIFYCMD via upsmon.conf options.

    Has anyone been able to get this to work? I too tried to use mail.php but nothing ever happens. The same command in the console works just great.

    Can anyone think of another alternative method to facilitate basic email functionality though something other than mail.php if we're hitting this limitation? As in, I wonder if we could still call a script via NOTIFYCMD but instead of the script relying on mail.php, it could use some other mechanism to send the email?

    This is so aggravating! Has anyone ever got NUT email functionality to work in any way-shape-form?


  • Rebel Alliance Developer Netgate

    FYI- Though NUT is not yet on 2.3, using mail.php from the GUI and elsewhere appears to work on 2.3 where it did not on 2.2.x. Might be the difference in PHP versions or other changes to PHP handling, but at least from the tests I was able to run, it sent messages to me when it wouldn't otherwise.



  • hi everybody,

    Can you tell me how to use mail.php under 2.3 release please. I’m looking for the syntax but without success. can you give me an example ? I try to send an email to the people who filled my php form.
    Thanks in advance

    cheers


  • Rebel Alliance Developer Netgate

    @Carltonson-sky:

    Can you tell me how to use mail.php under 2.3 release please. I’m looking for the syntax but without success. can you give me an example ? I try to send an email to the people who filled my php form.

    Not likely to be usable for that. It's for basic mail from the CLI:

    echo test | mail.php -s="Blah"
    

    Start a new thread in the appropriate forum (not here) and maybe someone can help come up with a way to do what you're after.

    Hosting a form like that on the firewall is not something we'd recommend, however.


Log in to reply