[Plugin] PortCheck - Check TCP port status

Questions and comments specific to a particular plugin should go here.
Post Reply
bengalih
Posts: 48
Joined: Sat Feb 25, 2012 1:13 am

[Plugin] PortCheck - Check TCP port status

Post by bengalih » Sat Sep 19, 2015 7:15 am

This is new plugin I wrote to provide more granular monitoring of host status.
I found that the ping plugin was not sufficient, as a remote host may be available, but the application listening on a specific port may have crashed.

This is pretty much my first foray into anything Python, and I wrote this plugin by cannibalizing the "Ping" plugin.
Anyone who wants to review for code efficiency is welcome, I'm sure there are many things that could be done better.
That being said, v.0.2 posted here works for me.

I've included here a sample tree to show how you could use this plugin (or at least one way).
The sample has two hosts configured in the managed (watch) list. When either becomes unavailable it sends an event which triggers an OSD display for 10 seconds showing which devices are offline.
Plugin has a lot of configurable options to control when and how often to throw events, as well as what to display as output.

I am currently using this to monitor:
Web server of XBMC
Network port of Onkyo receiver
Control port of Roku

This way, if any of these don't respond I can alert the main screen. Occasionally I have had these ports become unreachable even though the devices responded to pings.
In most cases, there will have to be manual intervention to rectify the offline issues, but at least now you can know exactly what is down.
Attachments
PortCheckExampleTree.xml
EG XML example
(1.49 KiB) Downloaded 57 times
__init__.py
PortCheck v 0.2.1
(18.52 KiB) Downloaded 84 times
Last edited by bengalih on Thu Oct 29, 2015 4:32 pm, edited 5 times in total.

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: PortCheck - Check TCP port status (Help still needed!!)

Post by krambriw » Sat Sep 19, 2015 8:52 am

Normally (without looking into your code) I would not let a thread go into such long sleep periods. Instead I would have shorter sleeps and a counter that increments for each sleep. Once the counter reaches a certain value, do the stuff. As example, set your sleep to 5 seconds, when the counter value reaches 180, do the action, reset the counter and start all over again.

Also, in threads, do not use time.sleep(x), instead inside the code of your thread, use typically mainThreadEvent.wait(5.0) to make the thread sleep for 5 seconds (name of thread is of course individual). In this way, your threads will respond faster when you need to terminate them. To terminate a thread, use mainThreadEvent.set().

Another alternative way would be not to use threads at all in your plugin. You could instead use the built in scheduler available in EG. With this you can schedule a function to be executed. Assume you have defined a function that does your stuff. The code for scheduling this to be executed after 15 seconds could be:

Code: Select all

def DoTheStuff(self):
    print 'go along and do the stuff'

self.taskObj = eg.scheduler.AddTask(
    15.0,
    self.DoTheStuff
)
If you keep track of the variable 'taskObj' holding the handle to the schedule, you can also add code to your plugin __stop__ function that terminates eventual pending schedules (to avoid EG hanging until all schedules have been executed) using code like this:

Code: Select all

try:
    eg.scheduler.CancelTask(self.taskObj)
except:
    pass


If you allow multiple schedules to be set, you need to keep track of all handles, I would add them to a list like 'self.taskObjectList' when they are created, and then at __stop__ do something like this:

Code: Select all

for i in self.taskObjectList:
    try:
        eg.scheduler.CancelTask(self.taskObjectList[i])
    except:
        pass
Last edited by krambriw on Tue Sep 22, 2015 3:55 am, edited 1 time in total.

bengalih
Posts: 48
Joined: Sat Feb 25, 2012 1:13 am

Re: PortCheck - Check TCP port status (Help still needed!!)

Post by bengalih » Sat Sep 19, 2015 2:15 pm

Thanks for the advice.

Much of what you said is greek to me as my learning at this point extends to the structures I've found in this plugin and googling a bit.
I will review these and see what I can pick up.

bengalih
Posts: 48
Joined: Sat Feb 25, 2012 1:13 am

Re: [Plugin] PortCheck - Check TCP port status

Post by bengalih » Mon Sep 21, 2015 10:59 pm

Moving to mainThreadEvent.wait seemed to have cleared up all the threading issue (delays and closing issues).
I've posted an update with this and other rough edges cleared up.

AFAIK this should work fine now for all.

Post Reply