Search code examples
awkmosquitto

mosquitto_pub returns 4 after some time


I have an UPS which have a proprietary protocol, so my only option is to take their 8-year old 32bit program and pipe it through an awk-script. ups_status produces a screen like this every two seconds or so.

      ----------===-----Welcome to ups_manager System!-----===-----
     Copyright(C) 2004 Richcomm Technologies, Inc.  May 16 2013 ver 1.1
+-----------------------------------------------------------------------------+
| UPS Factory:                  UPS Model:             UPS Version:           |
|---------------------------------++------------------------------------------|
| UPS Type           :   StandBY  || UPS Input Voltage: 227.00 Volt           |
| UPS Rating Voltage : 220.00 Volt||  |>>>>>>>>>>>>>>>>>>>>>>>-|----|----|----|
| UPS Rating Current :   3.00     || 180  190  200  210  220  230  240  250   |
| UPS Line Frequency :  50.00 Hz  ||                                          |
| Communication Port :      2     || UPS Output Voltage: 229.00    Volt       |
|                                 ||  |>>>>>>>>>>>>>>>>>>>>>>>>|----|----|----|
|     Input AC Power :  Normal    || 180  190  200  210  220  230   240  250  |
|     Battery Status :  Normal    ||                                          |
|     UPS Status     :  Normal    || UPS Power Loading:  18.00                |
|     Boost/Buck     :  Buck      ||  |>>>>|----|----|----|----|----|----|----|
|     UPS Temperature:   30.80    ||  0    20   40   60   80  100  120  140   |
|     UPS Self-Test  :  Normal    ||                                          |
|     Beeper Status  :  ON        ||  UPS Battery Level:  100.00              |
|                                 ||  |>>>>>>>>>>>>>>>>>>>>>>>>>----|----|----|
|                                 ||  0    20   40   60   80  100  120  140   |
|                                 ||                                          |
|                                 || UPS Input Frequency :  49.00 Hz          |
| ACfail Shutdown Delay : 3600 s  ||  |>>>>>>>>>>>>>>>>>>>>>>>>|----|----|----|
| UPS Turn Off Delay    :    2 min||  0    10   20   30   40   50   60   70   |
+---------------------------------++------------------------------------------+

I do ups_status | awk -f parseUPS.awk

parseUPS.awk parse the values and produce a json-string (named out) and do

  /Turn Off/ {
     out=sprintf(....);
     print out | "mosquitto_pub -h mqtt.local.net -t ups -l" 
    }

This works for a day or so, then mosquitto_pub switches to returning 4, invalid user or password.

If I restart the command, it functions correctly.

What could be happening?

Also could I capture the errorcode from the pipe, exit awk and put the line into a do while true -loop?


Solution

  • Ed Morton pointed me in the right direction of putting mosquitto_pub in the pipe instead of in awk.

    I tried ups_status |gawk -f parse.awk|mosquitto_pub -h mqtt.local.net -t ups2 -l
    This only gave output to mqtt after 4096 characters.

    Aha, buffering. So I added fflush("/dev/stdout"); after the printf in awk

    This resulted in the output came two lines at a time. Cause: buffering between ups_status and awk.

    The unbuffer-command from the expect package solved that.

    Final command:
    unbuffer ups_status |gawk -f parse.awk|mosquitto_pub -h mqtt.local.net -t ups2 -l

    This gives a nice mqtt-message every 3 seconds.