2016-11-04 18:50:38 +01:00
from __future__ import absolute_import
2016-10-31 15:17:51 +01:00
import time
import shlex
import threading
import subprocess
import bumblebee . module
import bumblebee . util
2016-10-31 16:08:03 +01:00
def usage ( ) :
return " dnf or dnf::<interval in seconds, defaults to 3600> "
def notes ( ) :
return " Spawns a separate thread that invokes ' dnf updateinfo ' every <interval> seconds. Critical status is if there is either more than 50 updates pending, or at least one of them is a security update. "
def description ( ) :
return " Checks DNF for updated packages and displays the number of <security>/<bugfixes>/<enhancements>/<other> pending updates. "
2016-10-31 15:17:51 +01:00
def get_dnf_info ( obj ) :
2016-11-05 14:37:07 +01:00
loops = obj . interval ( )
2016-11-04 18:50:38 +01:00
for thread in threading . enumerate ( ) :
if thread . name == " MainThread " :
main = thread
while main . is_alive ( ) :
loops + = 1
if loops < obj . interval ( ) :
2016-11-05 14:37:07 +01:00
time . sleep ( 1 )
2016-11-04 18:50:38 +01:00
continue
loops = 0
2016-11-04 19:01:19 +01:00
try :
res = subprocess . check_output ( shlex . split ( " dnf updateinfo " ) )
except Exception as e :
break
2016-10-31 15:17:51 +01:00
security = 0
bugfixes = 0
enhancements = 0
other = 0
for line in res . split ( " \n " ) :
2016-11-03 19:44:34 +01:00
if not line . startswith ( " " ) : continue
2016-10-31 15:17:51 +01:00
elif " ecurity " in line :
for s in str . split ( line ) :
if s . isdigit ( ) : security + = int ( s )
elif " ugfix " in line :
for s in str . split ( line ) :
if s . isdigit ( ) : bugfixes + = int ( s )
elif " hancement " in line :
for s in str . split ( line ) :
if s . isdigit ( ) : enhancements + = int ( s )
else :
for s in str . split ( line ) :
if s . isdigit ( ) : other + = int ( s )
obj . set ( " security " , security )
obj . set ( " bugfixes " , bugfixes )
obj . set ( " enhancements " , enhancements )
obj . set ( " other " , other )
class Module ( bumblebee . module . Module ) :
2016-11-05 14:37:07 +01:00
def __init__ ( self , output , config , alias ) :
super ( Module , self ) . __init__ ( output , config , alias )
2016-11-04 18:50:38 +01:00
2016-10-31 15:17:51 +01:00
self . _counter = { }
self . _thread = threading . Thread ( target = get_dnf_info , args = ( self , ) )
self . _thread . start ( )
def interval ( self ) :
2016-11-05 14:37:07 +01:00
return self . _config . parameter ( " interval " , 30 * 60 )
2016-10-31 15:17:51 +01:00
def set ( self , what , value ) :
self . _counter [ what ] = value
def get ( self , what ) :
return self . _counter . get ( what , 0 )
2016-11-05 14:37:07 +01:00
def widgets ( self ) :
2016-10-31 15:17:51 +01:00
result = [ ]
2016-11-03 19:44:34 +01:00
for t in [ " security " , " bugfixes " , " enhancements " , " other " ] :
2016-10-31 15:17:51 +01:00
result . append ( str ( self . get ( t ) ) )
2016-11-05 14:37:07 +01:00
return bumblebee . output . Widget ( self , " / " . join ( result ) )
2016-10-31 15:17:51 +01:00
2016-11-05 14:37:07 +01:00
def state ( self , widget ) :
2016-10-31 15:17:51 +01:00
total = sum ( self . _counter . values ( ) )
if total == 0 : return " good "
return " default "
2016-11-05 14:37:07 +01:00
def warning ( self , widget ) :
2016-10-31 15:17:51 +01:00
total = sum ( self . _counter . values ( ) )
return total > 0
2016-11-05 14:37:07 +01:00
def critical ( self , widget ) :
2016-10-31 15:17:51 +01:00
total = sum ( self . _counter . values ( ) )
return total > 50 or self . _counter . get ( " security " , 0 ) > 0
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4