views:

83

answers:

1

I'm developing a script which monitors a service for failure and launches another a different action depending on if failure is present or not.

I require a python script to monitor the output from a python program "monitor-services" and parsing the output search for an occurance of the word "failure". If present, the script should return with a true value, and run for a maxiumum of 30 seconds returning false if no occurance of "failure" occurs.

Sample output returned from "monitor-services":

{Device} [/device/xxx] Networks = dbus.Array([dbus.ObjectPath('/device/xxx/xxx'), dbus.ObjectPath('/device/00242b2e41b6/hidden')], signature=dbus.Signature('o'), variant_level=1)
{Service} [/profile/default/wifi_xxx_managed_wep] State = association
{Profile} [/profile/default] Services = dbus.Array([dbus.ObjectPath('/profile/default/wifi_xxx_managed_wep'), dbus.ObjectPath('/profile/default/wifi_xxx_managed_rsn')], signature=dbus.Signature('o'), variant_level=1)
{Manager} [/] Services = dbus.Array([dbus.ObjectPath('/profile/default/wifi_xxx_managed_wep'), dbus.ObjectPath('/profile/default/wifi_xxx_managed_rsn')], signature=dbus.Signature('o'), variant_level=1)
{Service} [/profile/default/wifi_xxx_managed_wep] **failure**
{Service} [/profile/default/wifi_xxx_managed_wep] State = idle

Any help would be appreciated.

[edit] A failure is likely to occur with 30 seconds or so of the action triggering the script, hence the script is required to terminate after 30 seconds. [/edit]

+1  A: 
#!/usr/bin/python
from subprocess import Popen, PIPE
import sys

data = Popen(["monitor-services"], stdout=PIPE).communicate()[0]

sys.exit("failure" in data)

This does everything you want, except for the 30s wait (which I don't understand). Notice that it returns 0 if failure is not found, 1 if it is found, according to the shell conventions (i.e. 0 is success, non-zero is failure).

Martin v. Löwis