<s> import logging <EOL> import warnings <EOL> import requests <EOL> from requests . exceptions import RequestException <EOL> from requests . packages . urllib3 import exceptions <EOL> from django . conf import settings as django_settings <EOL> from django . utils import six <EOL> import pyzabbix <EOL> from nodeconductor . monitoring . zabbix . errors import ZabbixError <EOL> from nodeconductor . monitoring . zabbix . log import ZabbixLogsFilter <EOL> logger = logging . getLogger ( __name__ ) <EOL> pyzabbix . logger . addFilter ( ZabbixLogsFilter ( ) ) <EOL> def _exception_decorator ( message , fail_silently = None ) : <EOL> def decorator ( func ) : <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> return func ( self , * args , ** kwargs ) <EOL> except ( pyzabbix . ZabbixAPIException , RequestException ) as exception : <EOL> if not self . _settings . get ( '<STR_LIT>' , False ) : <EOL> exception_name = exception . __class__ . __name__ <EOL> message_args = ( self , ) + args + tuple ( kwargs . values ( ) ) <EOL> logger . exception ( message . format ( * message_args , exception = exception , exception_name = exception_name ) ) <EOL> six . reraise ( ZabbixError , exception ) <EOL> return wrapper <EOL> return decorator <EOL> class QuietSession ( requests . Session ) : <EOL> """<STR_LIT>""" <EOL> def request ( self , * args , ** kwargs ) : <EOL> if not kwargs . get ( '<STR_LIT>' , self . verify ) : <EOL> with warnings . catch_warnings ( ) : <EOL> if hasattr ( exceptions , '<STR_LIT>' ) : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , exceptions . InsecurePlatformWarning ) <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , exceptions . InsecureRequestWarning ) <EOL> return super ( QuietSession , self ) . request ( * args , ** kwargs ) <EOL> else : <EOL> return super ( QuietSession , self ) . request ( * args , ** kwargs ) <EOL> class ZabbixApiClient ( object ) : <EOL> def __init__ ( self , settings = None ) : <EOL> self . _settings = settings <EOL> if settings is None : <EOL> try : <EOL> self . _settings = getattr ( django_settings , '<STR_LIT>' , { } ) [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise ZabbixError ( '<STR_LIT>' ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def get_host ( self , instance ) : <EOL> api = self . get_zabbix_api ( ) <EOL> name = self . get_host_name ( instance ) <EOL> hosts = api . host . get ( filter = { '<STR_LIT:host>' : name } ) <EOL> if not hosts : <EOL> raise ZabbixError ( '<STR_LIT>' % instance ) <EOL> return hosts [ <NUM_LIT:0> ] <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def get_host_ids ( self , instances ) : <EOL> """<STR_LIT>""" <EOL> api = self . get_zabbix_api ( ) <EOL> names = [ self . get_host_name ( instance ) for instance in instances ] <EOL> hosts = api . host . get ( filter = { '<STR_LIT:host>' : names } ) <EOL> if not hosts : <EOL> raise ZabbixError ( '<STR_LIT>' % instances ) <EOL> if len ( hosts ) != len ( names ) : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return [ host [ '<STR_LIT>' ] for host in hosts ] <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def create_host ( self , instance , warn_if_host_exists = True , is_tenant = False ) : <EOL> api = self . get_zabbix_api ( ) <EOL> kwargs = { } <EOL> try : <EOL> if not is_tenant : <EOL> kwargs [ '<STR_LIT>' ] = self . _settings [ '<STR_LIT>' ] <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = self . _settings [ '<STR_LIT>' ] <EOL> if not is_tenant and instance . template . application_type : <EOL> kwargs [ '<STR_LIT>' ] = self . _settings . get ( <EOL> "<STR_LIT>" % instance . template . application_type . slug . lower ( ) ) <EOL> if is_tenant : <EOL> kwargs [ '<STR_LIT>' ] = self . get_host_name ( instance , is_tenant ) <EOL> kwargs [ '<STR_LIT>' ] = self . get_host_visible_name ( instance , is_tenant ) <EOL> except KeyError as e : <EOL> logger . error ( '<STR_LIT>' , e ) <EOL> raise ZabbixError ( '<STR_LIT>' % e ) <EOL> _ , created = self . get_or_create_host ( <EOL> api , instance , <EOL> groupid = self . _settings [ '<STR_LIT>' ] , <EOL> interface_parameters = self . _settings [ '<STR_LIT>' ] , <EOL> ** kwargs <EOL> ) <EOL> if created : <EOL> object_name = '<STR_LIT>' if is_tenant else '<STR_LIT>' <EOL> logger . info ( '<STR_LIT>' , object_name , instance ) <EOL> if not created and warn_if_host_exists : <EOL> logger . warn ( '<STR_LIT>' , instance ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def update_host_visible_name ( self , host , is_tenant = False ) : <EOL> name = self . get_host_name ( host , is_tenant ) <EOL> visible_name = self . get_host_visible_name ( host , is_tenant ) <EOL> api = self . get_zabbix_api ( ) <EOL> hosts = api . host . get ( filter = { '<STR_LIT:host>' : name } ) <EOL> if hosts : <EOL> host_id = hosts [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> api . host . update ( { "<STR_LIT>" : host_id , <EOL> "<STR_LIT:name>" : visible_name } ) <EOL> logger . debug ( '<STR_LIT>' , host ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' , host ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def delete_host ( self , instance ) : <EOL> api = self . get_zabbix_api ( ) <EOL> deleted = self . delete_host_if_exists ( api , instance ) <EOL> if not deleted : <EOL> logger . warn ( '<STR_LIT>' , instance ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def create_hostgroup ( self , project ) : <EOL> api = self . get_zabbix_api ( ) <EOL> _ , created = self . get_or_create_hostgroup ( api , project ) <EOL> if not created : <EOL> logger . warn ( '<STR_LIT>' , project ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def delete_hostgroup ( self , project ) : <EOL> api = self . get_zabbix_api ( ) <EOL> deleted = self . delete_hostgroup_if_exists ( api , project ) <EOL> if not deleted : <EOL> logger . warn ( '<STR_LIT>' , project ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def create_service ( self , instance , hostid = None , warn_if_service_exists = True ) : <EOL> api = self . get_zabbix_api ( ) <EOL> service_parameters = self . _settings [ '<STR_LIT>' ] <EOL> name = self . get_service_name ( instance ) <EOL> service_parameters [ '<STR_LIT:name>' ] = name <EOL> if hostid is None : <EOL> hostid = self . get_host ( instance ) [ '<STR_LIT>' ] <EOL> service_parameters [ '<STR_LIT>' ] = self . get_host_sla_triggerid ( api , hostid ) <EOL> _ , created = self . get_or_create_service ( api , service_parameters ) <EOL> if not created and warn_if_service_exists : <EOL> logger . warn ( <EOL> '<STR_LIT>' % <EOL> ( instance , name ) <EOL> ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def delete_service ( self , instance ) : <EOL> api = self . get_zabbix_api ( ) <EOL> deleted = self . delete_service_if_exists ( api , instance ) <EOL> if not deleted : <EOL> logger . warn ( '<STR_LIT>' , instance ) <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def get_current_service_sla ( self , instance , start_time , end_time ) : <EOL> service_name = self . get_service_name ( instance ) <EOL> api = self . get_zabbix_api ( ) <EOL> service_data = api . service . get ( filter = { '<STR_LIT:name>' : service_name } ) <EOL> if len ( service_data ) != <NUM_LIT:1> : <EOL> raise pyzabbix . ZabbixAPIException ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( service_name , len ( service_data ) , instance ) <EOL> ) <EOL> service_id = service_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> sla = api . service . getsla ( <EOL> filter = { '<STR_LIT>' : service_id } , <EOL> intervals = { '<STR_LIT>' : start_time , '<STR_LIT:to>' : end_time } <EOL> ) [ service_id ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> service = api . service . get ( output = '<STR_LIT>' , serviceids = service_id ) [ <NUM_LIT:0> ] <EOL> service_trigger_id = service [ '<STR_LIT>' ] <EOL> events = self . get_trigger_events ( api , service_trigger_id , start_time , end_time ) <EOL> return sla , events <EOL> def get_zabbix_api ( self ) : <EOL> unsafe_session = QuietSession ( ) <EOL> unsafe_session . verify = False <EOL> api = pyzabbix . ZabbixAPI ( server = self . _settings [ '<STR_LIT>' ] , session = unsafe_session ) <EOL> api . login ( self . _settings [ '<STR_LIT:username>' ] , self . _settings [ '<STR_LIT:password>' ] ) <EOL> return api <EOL> def get_host_name ( self , host , is_tenant = False ) : <EOL> if is_tenant : <EOL> return '<STR_LIT:%s>' % host . tenant_id <EOL> return '<STR_LIT:%s>' % host . backend_id <EOL> def get_host_visible_name ( self , host , is_tenant = False ) : <EOL> if is_tenant : <EOL> return ( '<STR_LIT>' % ( host . tenant_id , host . project . name ) ) [ : <NUM_LIT:64> ] <EOL> return ( '<STR_LIT>' % ( host . uuid , host . name ) ) [ : <NUM_LIT:64> ] <EOL> def get_hostgroup_name ( self , project ) : <EOL> return '<STR_LIT>' % ( project . name , project . uuid ) <EOL> def get_service_name ( self , instance ) : <EOL> return '<STR_LIT>' % instance . backend_id <EOL> def get_host_sla_triggerid ( self , api , hostid ) : <EOL> try : <EOL> return api . trigger . get ( hostids = hostid , output = [ '<STR_LIT>' ] , <EOL> filter = { '<STR_LIT:description>' : '<STR_LIT>' } ) [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> except IndexError : <EOL> raise pyzabbix . ZabbixAPIException ( '<STR_LIT>' % hostid ) <EOL> def get_or_create_hostgroup ( self , api , project ) : <EOL> group_name = self . get_hostgroup_name ( project ) <EOL> if not api . hostgroup . exists ( name = group_name ) : <EOL> groups = api . hostgroup . create ( { '<STR_LIT:name>' : group_name } ) <EOL> return { '<STR_LIT>' : groups [ '<STR_LIT>' ] [ <NUM_LIT:0> ] } , True <EOL> else : <EOL> return api . hostgroup . get ( filter = { '<STR_LIT:name>' : group_name } ) [ <NUM_LIT:0> ] , False <EOL> def delete_hostgroup_if_exists ( self , api , project ) : <EOL> group_name = self . get_hostgroup_name ( project ) <EOL> try : <EOL> hostgroupid = api . hostgroup . get ( filter = { '<STR_LIT:name>' : group_name } ) [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> api . hostgroup . delete ( hostgroupid ) <EOL> return True <EOL> except IndexError : <EOL> return False <EOL> @ _exception_decorator ( '<STR_LIT>' ) <EOL> def get_or_create_host ( <EOL> self , api , instance , groupid , templateid , interface_parameters , <EOL> application_templateid = None , host_name = None , visible_name = None ) : <EOL> name = self . get_host_name ( instance ) if host_name is None else host_name <EOL> if name . strip ( ) == '<STR_LIT>' : <EOL> logger . warn ( '<STR_LIT>' , instance ) <EOL> raise pyzabbix . ZabbixAPIException ( '<STR_LIT>' ) <EOL> visible_name = self . get_host_visible_name ( instance ) if visible_name is None else visible_name <EOL> if not api . host . exists ( host = name ) : <EOL> templates = [ { "<STR_LIT>" : templateid } ] <EOL> if application_templateid : <EOL> templates . append ( { "<STR_LIT>" : application_templateid } ) <EOL> host = api . host . create ( { <EOL> "<STR_LIT:host>" : name , <EOL> "<STR_LIT:name>" : visible_name , <EOL> "<STR_LIT>" : [ interface_parameters ] , <EOL> "<STR_LIT>" : [ { "<STR_LIT>" : groupid } ] , <EOL> "<STR_LIT>" : templates , <EOL> } ) <EOL> return host , True <EOL> else : <EOL> return api . host . get ( filter = { '<STR_LIT:host>' : name } ) [ <NUM_LIT:0> ] , False <EOL> def delete_host_if_exists ( self , api , instance ) : <EOL> name = self . get_host_name ( instance ) <EOL> try : <EOL> hostid = api . host . get ( filter = { '<STR_LIT:host>' : name } ) [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> api . host . delete ( hostid ) <EOL> return True <EOL> except IndexError : <EOL> return False <EOL> def get_or_create_service ( self , api , service_parameters ) : <EOL> try : <EOL> service = api . service . get ( filter = { '<STR_LIT:name>' : service_parameters [ '<STR_LIT:name>' ] } ) [ <NUM_LIT:0> ] <EOL> return service , False <EOL> except IndexError : <EOL> return api . service . create ( service_parameters ) , True <EOL> def delete_service_if_exists ( self , api , instance ) : <EOL> name = self . get_service_name ( instance ) <EOL> try : <EOL> serviceid = api . service . get ( filter = { '<STR_LIT:name>' : name } ) [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> api . service . delete ( serviceid ) <EOL> return True <EOL> except IndexError : <EOL> return False <EOL> def get_trigger_events ( self , api , trigger_id , start_time , end_time ) : <EOL> event_data = api . event . get ( <EOL> output = '<STR_LIT>' , <EOL> objectids = trigger_id , <EOL> time_from = start_time , <EOL> time_till = end_time , <EOL> sortfield = [ "<STR_LIT>" ] , <EOL> sortorder = "<STR_LIT>" ) <EOL> return [ { '<STR_LIT>' : e [ '<STR_LIT>' ] , '<STR_LIT:value>' : e [ '<STR_LIT:value>' ] } for e in event_data ] </s>
