1+ from __future__ import division
12# Adagios is a web based Nagios configuration interface
23#
34# Copyright (C) 2014, Pall Sigurdsson <palli@opensource.is>
1516# You should have received a copy of the GNU Affero General Public License
1617# along with this program. If not, see <http://www.gnu.org/licenses/>.
1718
19+ from builtins import str
20+ from builtins import object
21+ from past .utils import old_div
1822from pynag .Utils import PynagError , defaultdict
1923
2024__author__ = 'palli'
@@ -122,7 +126,7 @@ def get_status(self):
122126 self .errors .append (
123127 _ ("We have not implemented how to use status method %s" ) % str (self .status_method ))
124128 return 3
125- except Exception , e :
129+ except Exception as e :
126130 self .errors .append (e )
127131 return 3
128132
@@ -137,14 +141,14 @@ def get_state_summary(self):
137141
138142 def get_all_states (self ):
139143 """ Returns a list of all subprocess states """
140- return map ( lambda x : x .get_status (), self .get_processes ())
144+ return [ x .get_status () for x in self .get_processes ()]
141145
142146 def get_worst_state (self ):
143147 """ Returns the worst state of any sub items
144148 """
145149 try :
146150 return int (max (self .get_all_states ()))
147- except Exception , e :
151+ except Exception as e :
148152 self .errors .append (e )
149153 return 3
150154
@@ -153,7 +157,7 @@ def get_best_state(self):
153157 """
154158 try :
155159 return int (min (self .get_all_states ()))
156- except Exception , e :
160+ except Exception as e :
157161 self .errors .append (e )
158162 return 3
159163
@@ -191,7 +195,7 @@ def run_business_rules(self):
191195 states = tags [tag ]
192196
193197 # Filter out states ok
194- states = filter ( lambda x : x > 0 , states )
198+ states = [ x for x in states if x > 0 ]
195199 if not states : # Nothing more to do
196200 continue
197201 if len (states ) >= num_problems :
@@ -255,7 +259,7 @@ def resolve_macrostring(self, string):
255259 all_macros = self .resolve_all_macros ()
256260 try :
257261 return string .format (** all_macros )
258- except KeyError , e :
262+ except KeyError as e :
259263 raise PynagError (_ ("Invalid macro in string. %s" ) % str (e ))
260264
261265 def resolve_macro (self , macroname , default = 'raise exception' ):
@@ -288,23 +292,23 @@ def resolve_macro(self, macroname, default='raise exception'):
288292 elif macroname == 'percent_state_0' :
289293 if len (self .get_all_states ()) == 0 :
290294 return 0
291- return 100.0 * state_summary [0 ] / sum (state_summary )
295+ return old_div ( 100.0 * state_summary [0 ], sum (state_summary ) )
292296 elif macroname == 'percent_state_1' :
293297 if len (self .get_all_states ()) == 0 :
294298 return 0
295- return 100.0 * state_summary [1 ] / sum (state_summary )
299+ return old_div ( 100.0 * state_summary [1 ], sum (state_summary ) )
296300 elif macroname == 'percent_state_2' :
297301 if len (self .get_all_states ()) == 0 :
298302 return 0
299- return 100.0 * state_summary [2 ] / sum (state_summary )
303+ return old_div ( 100.0 * state_summary [2 ], sum (state_summary ) )
300304 elif macroname == 'percent_state_3' :
301305 if len (self .get_all_states ()) == 0 :
302306 return 0
303- return 100.0 * state_summary [3 ] / sum (state_summary )
307+ return old_div ( 100.0 * state_summary [3 ], sum (state_summary ) )
304308 elif macroname == 'percent_problems' :
305309 if len (self .get_all_states ()) == 0 :
306310 return 0
307- return 100.0 * sum (state_summary [1 :]) / sum (state_summary )
311+ return old_div ( 100.0 * sum (state_summary [1 :]), sum (state_summary ) )
308312 elif macroname == 'current_state' :
309313 return self .get_status ()
310314 elif macroname == 'friendly_state' :
@@ -372,8 +376,7 @@ def remove_pnp_graph(self, host_name, service_description, metric_name):
372376 data ['metric_name' ] = metric_name
373377 if not self .graphs :
374378 return
375- self .graphs = filter (
376- lambda x : frozenset (x ) != frozenset (data ), self .graphs )
379+ self .graphs = [x for x in self .graphs if frozenset (x ) != frozenset (data )]
377380
378381 def get_pnp_last_value (self , host_name , service_description , metric_name ):
379382 """ Looks up current nagios perfdata via mk-livestatus and returns the last value for a specific metric (str)
@@ -572,7 +575,7 @@ def get_status(self):
572575 if service_status == 3 :
573576 return 2
574577 return service_status
575- except Exception , e :
578+ except Exception as e :
576579 self .errors .append (e )
577580 return 3
578581
@@ -583,11 +586,8 @@ def get_processes(self):
583586 else :
584587 services = self ._livestatus .get_services (
585588 'Filter: host_groups >= %s' % self .name )
586- livestatus_objects = map (
587- lambda x : [x .get ('host_name' ) + '/' + x .get (
588- 'description' ), x .get ('state' )],
589- services
590- )
589+ livestatus_objects = [[x .get ('host_name' ) + '/' + x .get (
590+ 'description' ), x .get ('state' )] for x in services ]
591591 for i in livestatus_objects :
592592 process = BusinessProcess (i [0 ])
593593 process .get_status = lambda : i [1 ]
@@ -667,7 +667,7 @@ def get_status(self):
667667 try :
668668 self .load ()
669669 return self ._service .get ('state' , 3 )
670- except Exception , e :
670+ except Exception as e :
671671 self .errors .append (e )
672672 return 3
673673
@@ -687,7 +687,7 @@ def load(self):
687687 def get_status (self ):
688688 try :
689689 self .load ()
690- except Exception , e :
690+ except Exception as e :
691691 self .errors .append (e )
692692 return 3
693693 method = self .status_method
@@ -753,7 +753,7 @@ def create_host(self):
753753 self .host_not_found = True
754754 self .errors .append (_ ("Host not found: " ) % self .name )
755755 all_hosts = pynag .Model .Host .objects .all
756- all_hosts = map ( lambda x : x .host_name , all_hosts )
756+ all_hosts = [ x .host_name for x in all_hosts ]
757757 if self .name not in all_hosts :
758758 host = pynag .Model .Host (use = "generic-domain" , host_name = self .name , address = self .name )
759759 host .action_url = "http://%s" % self .name
@@ -819,7 +819,7 @@ def get_all_json(filename=None):
819819 raw_data = None
820820 try :
821821 raw_data = open (filename , 'r' ).read ()
822- except IOError , e :
822+ except IOError as e :
823823 if e .errno == 2 : # File does not exist
824824 return []
825825 if not raw_data :
@@ -834,7 +834,7 @@ def get_all_processes(filename=None):
834834 result = []
835835 try :
836836 json_data = get_all_json (filename = filename )
837- except IOError , e :
837+ except IOError as e :
838838 if e .errno == 2 :
839839 json_data = []
840840 else :
@@ -848,7 +848,7 @@ def get_all_processes(filename=None):
848848def get_all_process_names (filename = None ):
849849 """ Return a list of all process names out there
850850 """
851- return map ( lambda x : x .name , get_all_processes (filename = filename ))
851+ return [ x .name for x in get_all_processes (filename = filename )]
852852
853853
854854def get_business_process (process_name , process_type = None , ** kwargs ):
@@ -862,13 +862,13 @@ def get_business_process(process_name, process_type=None, **kwargs):
862862 my_business_process = BPClass (process_name )
863863 try :
864864 my_business_process .load ()
865- except Exception , e :
865+ except Exception as e :
866866 my_business_process .errors .append (e )
867867 my_business_process .data .update (kwargs )
868868 return my_business_process
869869
870870
871- class PNP4NagiosGraph :
871+ class PNP4NagiosGraph ( object ) :
872872
873873 """ Represents one single PNP 4 nagios graph
874874 """
@@ -885,5 +885,5 @@ def get_image_urls(self):
885885 'json' , host = self .host_name , srv = self .service_description )
886886 graphs = json .loads (json_str )
887887 # only use graphs with same label
888- graphs = filter ( lambda x : x ['ds_name' ] == self .label , graphs )
888+ graphs = [ x for x in graphs if x ['ds_name' ] == self .label ]
889889 return graphs
0 commit comments