Script for Monit monitored application/proces status

Python and python framework

Moderator: leecollings

Post Reply
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Script for Monit monitored application/proces status

Post by Chris12 »

Hi,

I'm completely new to scripting, but willing to learn!

I want to know how should I approach the following:
- read out values from monit xml or json output so I can see status of monit monitored applications/processes.
- monit runs on a remoet/sperate Rpi n the same network
- domoticz runs on my Synology NAS, with dashticz to get some nice overviews.

I mainly want to have a visual of the status of the remote Rpi.
Maybe someone allready build a plugin like this, I searched for that but could not find it, the one thing found was a discontinued plugin (PiMonitor), anda plugin which I cannot get to work (Pi RemoteMonitor) as I cannot get 'python3-lxml' working on my NAS.

Or someone can point me to some examples doing the same for other json/xml information collecting.

Is python the correct language to get this working, or should I better foccus on a lua script or dventz?
It looks like the json output

Example of output in json and xml below:

json:

Code: Select all

Monit 5.25.2 uptime: 2d 8h 18m

[1;36mRemote Host 'pihole-FTL'[0m
  status                       [0;92mOK[0m
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  port response time           [0;39m0.882 ms to 127.0.0.1:4711 type TCP/IP protocol DEFAULT[0m
  data collected               [0;39mSun, 24 Jan 2021 21:15:47[0m

[1;36mRemote Host 'dnsmasq'[0m
  status                       [0;92mOK[0m
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  port response time           [0;39m1.202 s to 127.0.0.1:53 type UDP/IP protocol DEFAULT[0m
  data collected               [0;39mSun, 24 Jan 2021 21:15:49[0m

[1;36mRemote Host 'lighttpd'[0m
  status                       [0;92mOK[0m
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  port response time           [0;39m0.456 ms to 127.0.0.1:80 type TCP/IP protocol DEFAULT[0m
  data collected               [0;39mSun, 24 Jan 2021 21:15:49[0m

[1;36mSystem 'raspberrypi'[0m
  status                       [0;92mOK[0m
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0;39m[0.00] [0.00] [0.00][0m
  cpu                          [0;39m0.1%us 0.1%sy 0.1%wa[0m
  memory usage                 [0;39m79.7 MB [8.6%][0m
  swap usage                   [0;39m0 B [0.0%][0m
  uptime                       [0;39m2d 9h 41m[0m
  boot time                    [0;39mFri, 22 Jan 2021 11:35:24[0m
  data collected               [0;39mSun, 24 Jan 2021 21:15:49[0m

xml:

Code: Select all

<monit>
<server>
<id>4d3b3997da6a796e039e2916323e5b78</id>
<incarnation>1611315337</incarnation>
<version>5.25.2</version>
<uptime>238280</uptime>
<poll>300</poll>
<startdelay>300</startdelay>
<localhostname>raspberrypi</localhostname>
<controlfile>/etc/monit/monitrc</controlfile>
<httpd>
<address>192.168.1.50</address>
<port>2812</port>
<ssl>0</ssl>
</httpd>
</server>
<platform>
<name>Linux</name>
<release>4.19.75-v7+</release>
<version>#1270 SMP Tue Sep 24 18:45:11 BST 2019</version>
<machine>armv7l</machine>
<cpu>4</cpu>
<memory>948304</memory>
<swap>102396</swap>
</platform>
<service type="4">
<name>pihole-FTL</name>
<collected_sec>1611558493</collected_sec>
<collected_usec>754043</collected_usec>
<status>0</status>
<status_hint>0</status_hint>
<monitor>1</monitor>
<monitormode>0</monitormode>
<onreboot>0</onreboot>
<pendingaction>0</pendingaction>
<port>
<hostname>127.0.0.1</hostname>
<portnumber>4711</portnumber>
<request></request>
<protocol>DEFAULT</protocol>
<type>TCP</type>
<responsetime>0.000800</responsetime>
</port>
</service>
<service type="4">
<name>dnsmasq</name>
<collected_sec>1611558494</collected_sec>
<collected_usec>955541</collected_usec>
<status>0</status>
<status_hint>0</status_hint>
<monitor>1</monitor>
<monitormode>0</monitormode>
<onreboot>0</onreboot>
<pendingaction>0</pendingaction>
<port>
<hostname>127.0.0.1</hostname>
<portnumber>53</portnumber>
<request></request>
<protocol>DEFAULT</protocol>
<type>UDP</type>
<responsetime>1.201442</responsetime>
</port>
</service>
<service type="4">
<name>lighttpd</name>
<collected_sec>1611558494</collected_sec>
<collected_usec>956003</collected_usec>
<status>0</status>
<status_hint>0</status_hint>
<monitor>1</monitor>
<monitormode>0</monitormode>
<onreboot>0</onreboot>
<pendingaction>0</pendingaction>
<port>
<hostname>127.0.0.1</hostname>
<portnumber>80</portnumber>
<request></request>
<protocol>DEFAULT</protocol>
<type>TCP</type>
<responsetime>0.000415</responsetime>
</port>
</service>
<service type="5">
<name>raspberrypi</name>
<collected_sec>1611558494</collected_sec>
<collected_usec>956006</collected_usec>
<status>0</status>
<status_hint>0</status_hint>
<monitor>1</monitor>
<monitormode>0</monitormode>
<onreboot>0</onreboot>
<pendingaction>0</pendingaction>
<system>
<load>
<avg01>0.00</avg01>
<avg05>0.00</avg05>
<avg15>0.00</avg15>
</load>
<cpu>
<user>0.1</user>
<system>0.1</system>
<wait>0.1</wait>
</cpu>
<memory>
<percent>8.7</percent>
<kilobyte>82664</kilobyte>
</memory>
<swap>
<percent>0.0</percent>
<kilobyte>0</kilobyte>
</swap>
</system>
</service>
</monit>
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Monday 25 January 2021 8:30 Is Python the correct language to get this working, or should I better focus on a Lua script or dzVents?
Monit does not report in JSON (I believe M/Monit does but that is the paid version)

Probably you can do this in any script language.

Below is a proof of concept in dzVents. For now the data is only collected, converted from xml to a Lua table and dumped to the log.
It is not stored in domoticz devices or variables yet as I don't see in your post what data you want to present and in what kind of devices.

Code: Select all

--[[
        script to get monit data using call to _status API in xML format

        requires: dzVents >= 3.0.3 (domoticz >= V20202.2 build 11998)
]]--


local scriptVar = 'monit'

return
{
    on =
    {
        timer =
        {
            'every 5 minutes' -- just an example to trigger the request
        },
        devices =
        {
            'xmlTrigger', -- just for test development can be ignored
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        local monitSystem = '192.168.192.65' -- change to IP of monit monitSystem
        local monitPort = '2812'
        
        if not(item.isHTTPResponse) then
            dz.openURL(
            {
		url = 'http://' .. monitSystem .. ':' .. monitPort .. '/_status?format=xml',
                callback = scriptVar,
            })
        elseif item.isXML then
            dz.utils.dumpTable(item.xml) -- this will show the content of the return in the domoticz log
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

Thanks @waaren for the quick reply.

The two example xml and json output is actually my real monit output when doing:
http://<server IP>:2812/_status?format=json
http://<server IP>:2812/_status?format=xml

I'm not using the M/Monit but this version: monit 5.25.2
The data I want to use is for the RPi is basically the info collected in the json/xml:

Code: Select all

- (System name)
- status
- monitoring status
- monitoring mode
- load average
- cpu
- memory usage
- swap usage
- uptime
- boot time
- data collected

For the monitored processes:
- (Remote Host procesname)
- status
- monitoring status
- monitoring mode
- port response time
- data collected
So in domoticz I think a devices per item should be created isn't it ? It should be like:
<system name> - <data string>
<remote host processname> - <data string>
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Monday 25 January 2021 10:52 http://<server IP>:2812/_status?format=json
The format=json is ignored. You will see the same when you enter http://<server IP>:2812/_status

On a domoticz text device there is room for a limited number of lines. Can you please elaborate on what your idea is for displaying below information? Maybe drawing a picture on how you would like to see it help to understand.

Spoiler: show

Code: Select all

 monit:
2021-01-25 12:05:00.289  Status: dzVents: >     platform:
2021-01-25 12:05:00.289  Status: dzVents: >             release: 4.19.75-v7+
2021-01-25 12:05:00.289  Status: dzVents: >             machine: armv7l
2021-01-25 12:05:00.289  Status: dzVents: >             swap: 102396
2021-01-25 12:05:00.289  Status: dzVents: >             cpu: 4
2021-01-25 12:05:00.289  Status: dzVents: >             name: Linux
2021-01-25 12:05:00.289  Status: dzVents: >             version: #1270 SMP Tue Sep 24 18:45:11 BST 2019
2021-01-25 12:05:00.289  Status: dzVents: >             memory: 948304
2021-01-25 12:05:00.289  Status: dzVents: >     server:
2021-01-25 12:05:00.289  Status: dzVents: >             startdelay: 300
2021-01-25 12:05:00.289  Status: dzVents: >             incarnation: 1611315337
2021-01-25 12:05:00.289  Status: dzVents: >             version: 5.25.2
2021-01-25 12:05:00.289  Status: dzVents: >             id: 4d3b3997da6a796e039e2916323e5b78
2021-01-25 12:05:00.289  Status: dzVents: >             uptime: 238280
2021-01-25 12:05:00.289  Status: dzVents: >             httpd:
2021-01-25 12:05:00.289  Status: dzVents: >                     port: 2812
2021-01-25 12:05:00.289  Status: dzVents: >                     address: 192.168.1.50
2021-01-25 12:05:00.289  Status: dzVents: >                     ssl: 0
2021-01-25 12:05:00.289  Status: dzVents: >             controlfile: /etc/monit/monitrc
2021-01-25 12:05:00.289  Status: dzVents: >             localhostname: raspberrypi
2021-01-25 12:05:00.289  Status: dzVents: >             poll: 300
2021-01-25 12:05:00.289  Status: dzVents: >     service:
2021-01-25 12:05:00.289  Status: dzVents: >             1:
2021-01-25 12:05:00.289  Status: dzVents: >                     monitor: 1
2021-01-25 12:05:00.289  Status: dzVents: >                     name: pihole-FTL
2021-01-25 12:05:00.289  Status: dzVents: >                     _attr:
2021-01-25 12:05:00.289  Status: dzVents: >                             type: 4
2021-01-25 12:05:00.289  Status: dzVents: >                     status: 0
2021-01-25 12:05:00.289  Status: dzVents: >                     pendingaction: 0
2021-01-25 12:05:00.289  Status: dzVents: >                     collected_sec: 1611558493
2021-01-25 12:05:00.290  Status: dzVents: >                     onreboot: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     status_hint: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     port:
2021-01-25 12:05:00.290  Status: dzVents: >                             portnumber: 4711
2021-01-25 12:05:00.290  Status: dzVents: >                             type: TCP
2021-01-25 12:05:00.290  Status: dzVents: >                             protocol: DEFAULT
2021-01-25 12:05:00.290  Status: dzVents: >                             responsetime: 0.000800
2021-01-25 12:05:00.290  Status: dzVents: >                             hostname: 127.0.0.1
2021-01-25 12:05:00.290  Status: dzVents: >                             request:
2021-01-25 12:05:00.290  Status: dzVents: >                     monitormode: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     collected_usec: 754043
2021-01-25 12:05:00.290  Status: dzVents: >             2:
2021-01-25 12:05:00.290  Status: dzVents: >                     monitor: 1
2021-01-25 12:05:00.290  Status: dzVents: >                     name: dnsmasq
2021-01-25 12:05:00.290  Status: dzVents: >                     _attr:
2021-01-25 12:05:00.290  Status: dzVents: >                             type: 4
2021-01-25 12:05:00.290  Status: dzVents: >                     status: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     pendingaction: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     collected_sec: 1611558494
2021-01-25 12:05:00.290  Status: dzVents: >                     onreboot: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     status_hint: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     port:
2021-01-25 12:05:00.290  Status: dzVents: >                             portnumber: 53
2021-01-25 12:05:00.290  Status: dzVents: >                             type: UDP
2021-01-25 12:05:00.290  Status: dzVents: >                             protocol: DEFAULT
2021-01-25 12:05:00.290  Status: dzVents: >                             responsetime: 1.201442
2021-01-25 12:05:00.290  Status: dzVents: >                             hostname: 127.0.0.1
2021-01-25 12:05:00.290  Status: dzVents: >                             request:
2021-01-25 12:05:00.290  Status: dzVents: >                     monitormode: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     collected_usec: 955541
2021-01-25 12:05:00.290  Status: dzVents: >             3:
2021-01-25 12:05:00.290  Status: dzVents: >                     monitor: 1
2021-01-25 12:05:00.290  Status: dzVents: >                     name: lighttpd
2021-01-25 12:05:00.290  Status: dzVents: >                     _attr:
2021-01-25 12:05:00.290  Status: dzVents: >                             type: 4
2021-01-25 12:05:00.290  Status: dzVents: >                     status: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     pendingaction: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     collected_sec: 1611558494
2021-01-25 12:05:00.290  Status: dzVents: >                     onreboot: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     status_hint: 0
2021-01-25 12:05:00.290  Status: dzVents: >                     port:
2021-01-25 12:05:00.290  Status: dzVents: >                             portnumber: 80
2021-01-25 12:05:00.290  Status: dzVents: >                             type: TCP
2021-01-25 12:05:00.290  Status: dzVents: >                             protocol: DEFAULT
2021-01-25 12:05:00.290  Status: dzVents: >                             responsetime: 0.000415
2021-01-25 12:05:00.290  Status: dzVents: >                             hostname: 127.0.0.1
2021-01-25 12:05:00.291  Status: dzVents: >                             request:
2021-01-25 12:05:00.291  Status: dzVents: >                     monitormode: 0
2021-01-25 12:05:00.291  Status: dzVents: >                     collected_usec: 956003
2021-01-25 12:05:00.291  Status: dzVents: >             4:
2021-01-25 12:05:00.291  Status: dzVents: >                     system:
2021-01-25 12:05:00.291  Status: dzVents: >                             load:
2021-01-25 12:05:00.291  Status: dzVents: >                                     avg01: 0.00
2021-01-25 12:05:00.291  Status: dzVents: >                                     avg15: 0.00
2021-01-25 12:05:00.291  Status: dzVents: >                                     avg05: 0.00
2021-01-25 12:05:00.291  Status: dzVents: >                             swap:
2021-01-25 12:05:00.291  Status: dzVents: >                                     percent: 0.0
2021-01-25 12:05:00.291  Status: dzVents: >                                     kilobyte: 0
2021-01-25 12:05:00.291  Status: dzVents: >                             cpu:
2021-01-25 12:05:00.291  Status: dzVents: >                                     system: 0.1
2021-01-25 12:05:00.291  Status: dzVents: >                                     user: 0.1
2021-01-25 12:05:00.291  Status: dzVents: >                                     wait: 0.1
2021-01-25 12:05:00.291  Status: dzVents: >                             memory:
2021-01-25 12:05:00.291  Status: dzVents: >                                     percent: 8.7
2021-01-25 12:05:00.291  Status: dzVents: >                                     kilobyte: 82664
2021-01-25 12:05:00.291  Status: dzVents: >                     monitor: 1
2021-01-25 12:05:00.291  Status: dzVents: >                     name: raspberrypi
2021-01-25 12:05:00.291  Status: dzVents: >                     _attr:
2021-01-25 12:05:00.291  Status: dzVents: >                             type: 5
2021-01-25 12:05:00.291  Status: dzVents: >                     status: 0
2021-01-25 12:05:00.291  Status: dzVents: >                     pendingaction: 0
2021-01-25 12:05:00.291  Status: dzVents: >                     collected_sec: 1611558494
2021-01-25 12:05:00.291  Status: dzVents: >                     status_hint: 0
2021-01-25 12:05:00.291  Status: dzVents: >                     onreboot: 0
2021-01-25 12:05:00.291  Status: dzVents: >                     monitormode: 0
2021-01-25 12:05:00.291  Status: dzVents: >                     collected_usec: 956006


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

OK, so the script basically has an output of text. That's fine for me. It will show the needed info.
Do you mean with the limited number of lines per textdevice, the amount of text per item/idx or the total of the recieved info of a device?

Let me explain what I want to see per item, I will take Rpi system as an example as well as Pi-Hole FTL

RPi system:
- load avg 5 //average load per 5 min, I think the dvents script will read the Monit every 5 min is enough
- cpu system
- memory percent
- swap percent
- monitor // 1 = Active, I do not know the other status (maybe 0 as well for inactive?)
- status // 0 = monitored, I do not know the other status (pending/failed as well ?)
- name // is this needed for distinguising the monitored process/system?
- collected use seconds // this is the uptime I presume (maybe reformat the seconds to d/h/m/s format )

Pi-Hole FTL:
- monitor // 1 = Active, I do not know the other status (maybe 0 as well for inactive?)
- status // 0 = monitored, I do not know the other status (pending/failed as well ?)
- name // is this needed for distinguising the monitored process/system?
- port repsonsetime
- collected use seconds // this is the uptime I presume (maybe reformat the seconds to d/h/m/s format )

With the above info I want to create a dial in dashticz showing for example for Pi-Hole FTL:
Status = OK
Monitored = YES
(and maybe conditional format as well, green = O, red = not OK. but not sure a dial can do this)

For the Rpi it will be something like uptime/load avg/cpu etc. just to show the health of the system, in a normal graph.
(same script can later be used for monitoring other systems maybe as well)

And this information will then be fitted a a dashticz page, likely somewhere into the page below which allready shows detailed info of PiHole blockingall kind of adds.
Spoiler: show
Image
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Monday 25 January 2021 13:24 OK, so the script basically has an output of text. That's fine for me. It will show the needed info.
Do you mean with the limited number of lines per textdevice, the amount of text per item/idx or the total of the recieved info of a device?
I am not familiair with Dashticz but below script update these two text devices in domoticz.
monit.png
monit.png (30.19 KiB) Viewed 1623 times

Code: Select all

--[[
        script to get monit data using call to _status API in xML format
        requires: dzVents >= 3.0.3 (domoticz >= V20202.2 build 11998)
]]--


local scriptVar = 'monit'

return
{
    on =
    {
        timer =
        {
            'every 5 minutes' -- just an example to trigger the request
        },
        devices =
        {
            'xmlTrigger', -- just for test development can be ignored
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        local monitSystem = '192.168.192.65' -- change to IP of monit system
        local monitPort = 2812 -- change to portnumber for monit 
        local monitRPI = dz.devices('monitRPI') -- create as (virtual) text device
        local monitPI_Hole = dz.devices('monitPI-Hole') -- create as (virtual) text device

        local function updatePI_hole(st)
            local str = ''
            str = str .. 'name: '.. st.name .. '\n' ..
           'monitor: ' .. ( ( tonumber(st.monitor) == 1 and 'active' ) or 'inactive' ) .. '\n' ..
            'status: '.. st.status .. '\n' ..
            'port responsetime: ' ..  st.port.responsetime .. '\n' ..
            'collected micro seconds: '  .. st.collected_usec
            monitPI_Hole.updateText(str)
        end

        local function updateRaspberryPI(st)
            local str = ''
            str = str .. 'name: '.. st.name .. '\n' ..
            'load avg 5: ' .. st.system.load.avg05 .. '\n' ..
            'CPU system : '.. st.system.cpu.system .. '\n' ..
            'swap: ' ..  st.system.swap.percent .. '\n' ..
           'monitor: ' .. ( ( tonumber(st.monitor) == 1 and 'active' ) or 'inactive' ) .. '\n' ..
            'status: '.. st.status .. '\n' ..
            'collected micro seconds: '  .. st.collected_usec
            monitRPI.updateText(str)
        end

        local function updateDevices(t)
            for _, record in ipairs(t) do
                if record.name == 'pihole-FTL' then
                    updatePI_hole(record)
                elseif record.name == 'raspberrypi' then
                    updateRaspberryPI(record)
                end
            end
        end


 -- (Test only can be ignored) local xmlString =  '<monit><server><id>4d3b3997da6a796e039e2916323e5b78</id><incarnation>1611315337</incarnation><version>5.25.2</version><uptime>238280</uptime><poll>300</poll><startdelay>300</startdelay><localhostname>raspberrypi</localhostname><controlfile>/etc/monit/monitrc</controlfile><httpd><address>192.168.1.50</address><port>2812</port><ssl>0</ssl></httpd></server><platform><name>Linux</name><release>4.19.75-v7+</release><version>#1270 SMP Tue Sep 24 18:45:11 BST 2019</version><machine>armv7l</machine><cpu>4</cpu><memory>948304</memory><swap>102396</swap></platform><service type="4"><name>pihole-FTL</name><collected_sec>1611558493</collected_sec><collected_usec>754043</collected_usec><status>0</status><status_hint>0</status_hint><monitor>1</monitor><monitormode>0</monitormode><onreboot>0</onreboot><pendingaction>0</pendingaction><port><hostname>127.0.0.1</hostname><portnumber>4711</portnumber><request></request><protocol>DEFAULT</protocol><type>TCP</type><responsetime>0.000800</responsetime></port></service><service type="4"><name>dnsmasq</name><collected_sec>1611558494</collected_sec><collected_usec>955541</collected_usec><status>0</status><status_hint>0</status_hint><monitor>1</monitor><monitormode>0</monitormode><onreboot>0</onreboot><pendingaction>0</pendingaction><port><hostname>127.0.0.1</hostname><portnumber>53</portnumber><request></request><protocol>DEFAULT</protocol><type>UDP</type><responsetime>1.201442</responsetime></port></service><service type="4"><name>lighttpd</name><collected_sec>1611558494</collected_sec><collected_usec>956003</collected_usec><status>0</status><status_hint>0</status_hint><monitor>1</monitor><monitormode>0</monitormode><onreboot>0</onreboot><pendingaction>0</pendingaction><port><hostname>127.0.0.1</hostname><portnumber>80</portnumber><request></request><protocol>DEFAULT</protocol><type>TCP</type><responsetime>0.000415</responsetime></port></service><service type="5"><name>raspberrypi</name><collected_sec>1611558494</collected_sec><collected_usec>956006</collected_usec><status>0</status><status_hint>0</status_hint><monitor>1</monitor><monitormode>0</monitormode><onreboot>0</onreboot><pendingaction>0</pendingaction><system><load><avg01>0.00</avg01><avg05>0.00</avg05><avg15>0.00</avg15></load><cpu><user>0.1</user><system>0.1</system><wait>0.1</wait></cpu><memory><percent>8.7</percent><kilobyte>82664</kilobyte></memory><swap><percent>0.0</percent><kilobyte>0</kilobyte></swap></system></service></monit>'

        -- main code
        if not(item.isHTTPResponse) then
            dz.openURL(
            {
                url = 'http://' .. monitSystem .. ':' .. monitPort .. '/_status?format=xml',
                callback = scriptVar,
            })
        elseif item.isXML then
 -- (Test only can be ignored)             item.xml = dz.utils.fromXML(xmlString)
 -- (Test only can be ignored)             dz.utils.dumpTable(item.xml) -- this will show the content of the return in the domoticz log
            updateDevices(item.xml.monit.service)
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

Thank you! I will take a look at this later this evening.

Can the values of monitRPi be splitted into seperate text devices? So that I have one for each item (load/cpu/mem/status/monitor).
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Monday 25 January 2021 16:41 Thank you! I will take a look at this later this evening.

Can the values of monitRPi be splitted into seperate text devices? So that I have one for each item (load/cpu/mem/status/monitor).
Yes that is possible.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

Hi @waaren,

I loaded the script into domoticz, and created the 2 devices as mentioned in the script.
Looking at the logging there seems to be someting with a user/password, as that is not defined in the script yet.
Spoiler: show

Code: Select all

 2021-01-25 19:30:00.126 Status: dzVents: Info: monit: ------ Start internal script: Monit-status-data:, trigger: "every 2 minutes"
2021-01-25 19:30:00.138 Status: dzVents: Debug: monit: Processing device-adapter for monitRPI: Text device
2021-01-25 19:30:00.138 Status: dzVents: Debug: monit: Processing device-adapter for monitPI-Hole: Text device
2021-01-25 19:30:00.138 Status: dzVents: Debug: monit: OpenURL: url = http://192.168.1.50:2812/_status?format=xml
2021-01-25 19:30:00.138 Status: dzVents: Debug: monit: OpenURL: method = GET
2021-01-25 19:30:00.138 Status: dzVents: Debug: monit: OpenURL: post data = nil
2021-01-25 19:30:00.138 Status: dzVents: Debug: monit: OpenURL: headers = nil
2021-01-25 19:30:00.139 Status: dzVents: Debug: monit: OpenURL: callback = monit
2021-01-25 19:30:00.139 Status: dzVents: Info: monit: ------ Finished Monit-status-data
2021-01-25 19:30:01.682 Status: dzVents: Info: Handling httpResponse-events for: "monit"
2021-01-25 19:30:01.682 Status: dzVents: Info: monit: ------ Start internal script: Monit-status-data: HTTPResponse: "monit"
2021-01-25 19:30:01.696 Status: dzVents: Debug: monit: Processing device-adapter for monitRPI: Text device
2021-01-25 19:30:01.696 Status: dzVents: Debug: monit: Processing device-adapter for monitPI-Hole: Text device
2021-01-25 19:30:01.697 Status: dzVents: Debug: monit: {["isScene"]=false, ["statusText"]="[b]Unauthorized[/b]", ["callback"]="monit", ["isHardware"]=false, ["protocol"]="HTTP/1.0", ["dump"]=function, ["headers"]={["Content-Type"]="text/html", ["Content-Length"]="256", ["WWW-Authenticate"]="Basic realm="monit"", ["Content-Encoding"]="gzip", ["Connection"]="close", ["Date"]="Mon, 25 Jan 2021 18:30:00 GMT", ["Server"]="monit 5.25.2"}, ["isDevice"]=false, ["isGroup"]=false, ["data"]="<html><head><title>401 Unauthorized</title></head><body bgcolor=#FFFFFF><h2>[b]Unauthorized</h2>You are not authorized to access monit. Either you supplied the wrong credentials (e.g. bad password), or your browser doesn't understand how to supply the credentials required<hr><a href='http://mmonit.com/monit/'><font size=-1>monit 5.25.2</font></a></body></html>[/b]
2021-01-25 19:30:01.697 ", ["baseType"]="httpResponse", ["isJSON"]=false, ["ok"]=false, ["isXML"]=false, ["_contentType"]="text/html", ["isSystem"]=false, ["isTimer"]=false, ["isVariable"]=false, ["isHTTPResponse"]=true, ["trigger"]="monit", ["statusCode"]=401, ["isSecurity"]=false, ["isCustomEvent"]=false}
2021-01-25 19:30:01.697 Status: dzVents: Info: monit: ------ Finished Monit-status-data
2021-01-25 19:30:01.682 Error: dzVents: Error: (3.0.18) monit: HTTP/1.0 response: 401 ==>> Unauthorized
2021-01-25 19:30:01.696 Error: dzVents: Error: (3.0.18) monit: There was a problem handling the request 
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Monday 25 January 2021 19:36 I loaded the script into domoticz, and created the 2 devices as mentioned in the script.
Looking at the logging there seems to be someting with a user/password, as that is not defined in the script yet.
That is because of a setting in monitrc
I allowed all calls from my local LAN (192.168.192) in my monitrc to access monit without password.

Code: Select all

set daemon 60
set pidfile /usr/local/monit/var/monit.pid
set statefile /usr/local/monit/var/monit.state
set idfile /usr/local/monit/var/monit.id
set logfile /usr/local/monit/var/monit.log

set eventqueue
    basedir /usr/local/monit/var/events
    slots 100

set httpd port 2812
        allow localhost
        allow 192.168.192.0/255.255.255.0

#use address 0.0.0.0

#include files in subdir
include /usr/local/monit/var/monit.d/*


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

waaren wrote: Monday 25 January 2021 20:03
Chris12 wrote: Monday 25 January 2021 19:36 I loaded the script into domoticz, and created the 2 devices as mentioned in the script.
Looking at the logging there seems to be someting with a user/password, as that is not defined in the script yet.
That is because of a setting in monitrc
I allowed all calls from my local LAN (192.168.192) in my monitrc to access monit without password.

Code: Select all

set daemon 60
set pidfile /usr/local/monit/var/monit.pid
set statefile /usr/local/monit/var/monit.state
set idfile /usr/local/monit/var/monit.id
set logfile /usr/local/monit/var/monit.log

set eventqueue
    basedir /usr/local/monit/var/events
    slots 100

set httpd port 2812
        allow localhost
        allow 192.168.192.0/255.255.255.0

#use address 0.0.0.0

#include files in subdir
include /usr/local/monit/var/monit.d/*


OK in my monitrc I had an extra line with
allow admin:xxxxxx

I now have hashed that one out, and now the data is collected in domoticz.
Isn't there an option to set the username/password as well in the monit call ?

Second question:
I now have a line with all info collected from monit, how can I seperate that?

Code: Select all

name: raspberrypi load avg 5: 0.03 CPU system : 0.1 swap: 0.0 monitor: active status: 0 collected micro seconds: 985514
In monit itself it shows like:
Spoiler: show
Image
So somehow it doesn't show the used memory

and how to reformat the 'status: 0' to 'status: Monitored' is that in the script something like:

Code: Select all

 'status: ' .. ( ( tonumber(st.status) == 0 and 'Monitored' ) or 'NOT Monitored' ) .. '\n' ..
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Monday 25 January 2021 20:33 I now have hashed that one out, and now the data is collected in domoticz.
Isn't there an option to set the username/password as well in the monit call ?
The call is the same as if you would enter it from the address bar in your browser so probably something like
'h ttp://username:password@' .. monitSystem .. ':' .. monitPort .. '/_status?format=xml',
Second question:
I now have a line with all info collected from monit, how can I separate that?

Code: Select all

name: raspberrypi load avg 5: 0.03 CPU system : 0.1 swap: 0.0 monitor: active status: 0 collected micro seconds: 985514
The separation for a virtual text sensor is done by the \n (linefeed) I don't know how that is done for Dashticz
...So somehow it doesn't show the used memory
It is not shown because It only shows what you asked for. If you also need the memory you will need to change the function updateRaspberryPi() to

Code: Select all

        local function updateRaspberryPI(st)
            local str = ''
            str = str .. 'name: '.. st.name .. '\n' ..
            'load avg 5: ' .. st.system.load.avg05 .. '\n' ..
            'CPU system : '.. st.system.cpu.system .. '\n' ..
            'Memory  : '.. st.system.memory.kilobyte .. '\n' ..
            'swap: ' ..  st.system.swap.percent .. '\n' ..
            'monitor: ' .. ( tonumber(st.monitor) == 1 and 'active'  or 'inactive' ) .. '\n' ..
            'status: '.. st.status .. '\n' ..
            'collected micro seconds: '  .. st.collected_usec
            monitRPI.updateText(str)
        end
and how to reformat the 'status: 0' to 'status: Monitored' is that in the script something like:

Code: Select all

 'status: ' .. ( ( tonumber(st.status) == 0 and 'Monitored' ) or 'NOT Monitored' ) .. '\n' ..
Yes
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

OK, I rebuild the script a little bit so the user/password are local variables as well.

Code: Select all

        local monitUser = 'admin' -- change to user with acces to monit
        local monitPassword = 'monit' -- change to your password

         url = 'http://' .. monitUser .. ':' .. monitPassword .. '@' .. monitSystem .. ':' .. monitPort .. '/_status?format=xml', 
I also see the memory with the new addition you mentioned. But how to set it that it will show megabytes instead of kilobytes
(I tried change kilo to mega, but that gives a script error in the log)

Now that the script is working, how can I split the reported values into seperate devices. Like done with for example synology sensors
That creates 5 devices:
Spoiler: show
Image
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for Monit monitored application/proces status

Post by waaren »

Chris12 wrote: Tuesday 26 January 2021 8:01 How to set it that it will show megabytes instead of kilobytes
..... Ehhh; divide by 1024 ?
Now that the script is working, how can I split the reported values into separate devices.
Add lines

Code: Select all

        local monitRPIMemory = dz.devices('monitRPIMemory') -- create as (virtual) custom Sensor 
        local monitRPICPU = dz.devices('monitRPICPU') -- create as (virtual) percentage device
to the section where the devices are declared
and change function updateRaspberryPI to

Code: Select all

        local function updateRaspberryPI(st)
            local str = ''
            str = str .. 'name: '.. st.name .. '\n' ..
            'load avg 5: ' .. st.system.load.avg05 .. '\n' ..
            'CPU system : '.. st.system.cpu.system .. '\n' ..
             'Memory  : '.. dz.utils.round( st.system.memory.kilobyte / 1024 , 1 ) .. ' MB \n' ..
            'swap: ' ..  st.system.swap.percent .. '\n' ..
            'monitor: ' .. ( tonumber(st.monitor) == 1 and 'active'  or 'inactive' ) .. '\n' ..
            'status: ' .. ( ( tonumber(st.status) == 0 and 'Monitored' ) or 'NOT Monitored' ) .. '\n' ..
            'collected micro seconds: '  .. st.collected_usec
            
            -- Update the overal text devices
            monitRPI.updateText(str)
            
            -- Update the (percentage) CPU device
            monitRPICPU.updatePercentage(st.system.cpu.system)
            
            -- Update the (custom Sensor) memory in use device
            monitRPIMemory.updateCustomSensor(dz.utils.round( st.system.memory.kilobyte / 1024 , 1 ))
            
            -- add other device types here. See the dzVent update methods to be used in the dzVents wiki 
            
        end
Other devices for individual values can be done in the same way. Please read the dzVents wiki on what update methods are to be used for other device types.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by Chris12 »

Thanks! I will take a look at this later this afternoon.
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
newpond
Posts: 26
Joined: Sunday 26 September 2021 2:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script for Monit monitored application/proces status

Post by newpond »

does anyone know how I can pull the UPtime value out of the top section?


<monit>
<server>
<id>1b1eea27483*******************</id>
<incarnation>1666894067</incarnation>
<version>5.25.2</version>
<uptime>1394</uptime>
<poll>60</poll>
<startdelay>70</startdelay>
<localhostname>piz3</localhostname>
<controlfile>/etc/monit/monitrc</controlfile>
User avatar
waltervl
Posts: 5714
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Script for Monit monitored application/proces status

Post by waltervl »

newpond wrote: Thursday 27 October 2022 20:59 does anyone know how I can pull the UPtime value out of the top section?


<monit>
<server>
<id>1b1eea27483*******************</id>
<incarnation>1666894067</incarnation>
<version>5.25.2</version>
<uptime>1394</uptime>
<poll>60</poll>
<startdelay>70</startdelay>
<localhostname>piz3</localhostname>
<controlfile>/etc/monit/monitrc</controlfile>
To put in in a Domoticz device? Use the Domoticz http poller with lua xml parser. See https://www.domoticz.com/wiki/HTTP/HTTPS_poller
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
newpond
Posts: 26
Joined: Sunday 26 September 2021 2:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script for Monit monitored application/proces status

Post by newpond »

thanks for the reply!!
bit past my scripting level though. I managed to get info out of the service type section (under the pi server name) using your script (thanks for that) but can't see how to get the data out of the level above using the same script.
do I take it that's I not possible then and the other route you suggest is the only way?

cheers
Jon
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest