本文整理汇总了Python中shinken.db_mysql.DBMysql类的典型用法代码示例。如果您正苦于以下问题:Python DBMysql类的具体用法?Python DBMysql怎么用?Python DBMysql使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DBMysql类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init
def init(self):
logger.log("I connect to NDO database")
self.db = DBMysql(self.host, self.user, self.password, self.database,
self.character_set, table_prefix='nagios_', port=self.port)
self.connect_database()
# Cache for hosts and services
# The structure is as follow:
# First the instance id then the host / (host,service desc) to access the wanted data
self.services_cache_sync = {}
self.hosts_cache_sync = {}
# We need to search for centreon_specific fields, like long_output
query = u"select TABLE_NAME from information_schema.columns where TABLE_SCHEMA='ndo' and TABLE_NAME='nagios_servicestatus' and COLUMN_NAME='long_output';"
self.db.execute_query(query)
row = self.db.fetchone()
if row is None or len(row) < 1:
self.centreon_version = False
else:
self.centreon_version = True
logger.log("[MySQL/NDO] Using the centreon version")
# Cache for database id
# In order not to query the database every time
self.database_id_cache = {}
# Mapping service_id in Shinken and in database
# Because can't acces host_name from a service everytime :(
self.mapping_service_id = {}
# Todo list to manage brok
self.todo = []
示例2: init
def init(self):
from shinken.db_mysql import DBMysql
logger.info("[glpidb] Creating a mysql backend : %s (%s)" % (self.host, self.database))
self.db_backend = DBMysql(self.host, self.user, self.password, self.database, self.character_set)
logger.info("[glpidb] Connecting to database ...")
self.db_backend.connect_database()
logger.info("[glpidb] Connected")
示例3: init
def init(self):
print "I connect to NDO database"
self.db = DBMysql(self.host, self.user, self.password, self.database, self.character_set, table_prefix='nagios_')
self.connect_database()
#Cache for hosts and services
#will be flushed when we got a net instance id
#or something like that
self.services_cache = {}
self.hosts_cache = {}
示例4: TestConfig
class TestConfig(ShinkenTest):
# setUp is inherited from ShinkenTest
def create_db(self):
self.db = DBMysql(host='localhost', user='root', password='root', database='merlin', character_set='utf8')
def test_connect_database(self):
if not DBMysql:
return
self.create_db()
try:
self.db.connect_database()
except Exception: # arg, no database here? sic!
pass
def test_execute_query(self):
if not DBMysql:
return
self.create_db()
try:
self.db.connect_database()
q = "DELETE FROM service WHERE instance_id = '0'"
self.db.execute_query(q)
except Exception:
pass
示例5: __init__
def __init__(self, modconf, host=None, user=None, password=None, database=None, character_set=None, database_path=None):
#Mapping for name of data, rename attributes and transform function
self.mapping = {
#Host
'host_check_result' : {
'plugin_monitoring_services_id' : {'transform' : None},
'event' : {'transform' : None},
'perf_data' : {'transform' : None},
'output' : {'transform' : None},
'state' : {'transform' : None},
'latency' : {'transform' : None},
'execution_time' : {'transform' : None},
'state_type' : {'transform' : None},
},
#Service
'service_check_result' : {
'plugin_monitoring_services_id' : {'transform' : None},
'plugin_monitoring_servicescatalogs_id' : {'transform' : None},
'event' : {'transform' : None},
'perf_data' : {'transform' : None},
'output' : {'transform' : None},
'state' : {'transform' : None},
'latency' : {'transform' : None},
'execution_time' : {'transform' : None},
'state_type' : {'transform' : None},
}
}
# Last state of check
# self.checkstatus = {
# '0' : None,
# }
BaseModule.__init__(self, modconf)
self.host = host
self.user = user
self.password = password
self.database = database
self.character_set = character_set
self.database_path = database_path
from shinken.db_mysql import DBMysql
print "Creating a mysql backend"
self.db_backend = DBMysql(host, user, password, database, character_set)
示例6: Ndodb_Mysql_broker
class Ndodb_Mysql_broker(BaseModule):
def __init__(self, conf):
BaseModule.__init__(self, conf)
# Mapping for name of dataand transform function
self.mapping = {
"program_status": {
"program_start": {"name": "program_start_time", "transform": de_unixify},
"pid": {"name": "process_id", "transform": None},
"last_alive": {"name": "status_update_time", "transform": de_unixify},
"is_running": {"name": "is_currently_running", "transform": None},
}
}
self.host = conf.host
self.user = conf.user
self.password = conf.password
self.database = conf.database
self.character_set = conf.character_set
# Called by Broker so we can do init stuff
# TODO : add conf param to get pass with init
# Conf from arbiter!
def init(self):
print "I connect to NDO database"
self.db = DBMysql(
self.host, self.user, self.password, self.database, self.character_set, table_prefix="nagios_"
)
self.connect_database()
# Cache for hosts and services
# will be flushed when we got a net instance id
# or something like that
self.services_cache = {}
self.hosts_cache = {}
# Get a brok, parse it, and put in in database
# We call functions like manage_ TYPEOFBROK _brok that return us queries
def manage_brok(self, b):
# We've got problem with instance_id == 0 so we add 1 every where
if "instance_id" in b.data:
b.data["instance_id"] = b.data["instance_id"] + 1
# print "(Ndo) I search manager:", manager
queries = BaseModule.manage_brok(self, b)
if queries is not None:
for q in queries:
self.db.execute_query(q)
return
# print "(ndodb)I don't manage this brok type", b
# Create the database connection
# TODO : finish (begin :) ) error catch and conf parameters...
def connect_database(self):
self.db.connect_database()
def get_host_object_id_by_name(self, host_name):
# First look in cache.
if host_name in self.hosts_cache:
return self.hosts_cache[host_name]
# Not in cache, not good
query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='1'" % host_name
self.db.execute_query(query)
row = self.db.fetchone()
if row is None or len(row) < 1:
return 0
else:
self.hosts_cache[host_name] = row[0]
return row[0]
def get_hostgroup_object_id_by_name(self, hostgroup_name):
query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='3'" % hostgroup_name
self.db.execute_query(query)
row = self.db.fetchone()
if row is None or len(row) < 1:
return 0
else:
return row[0]
def get_service_object_id_by_name(self, host_name, service_description):
# first look in cache
if (host_name, service_description) in self.services_cache:
return self.services_cache[(host_name, service_description)]
# else; not in cache :(
query = u"SELECT object_id from nagios_objects where name1='%s' and name2='%s' and objecttype_id='2'" % (
host_name,
service_description,
)
self.db.execute_query(query)
row = self.db.fetchone()
if row is None or len(row) < 1:
return 0
else:
self.services_cache[(host_name, service_description)] = row[0]
return row[0]
def get_servicegroup_object_id_by_name(self, servicegroup_name):
query = u"SELECT object_id from nagios_objects where name1='%s' and objecttype_id='4'" % servicegroup_name
self.db.execute_query(query)
row = self.db.fetchone()
#.........这里部分代码省略.........
示例7: Ndodb_Mysql_broker
class Ndodb_Mysql_broker(BaseModule):
""" This Class is a plugin for the Shinken Broker. It is in charge
to brok information into the database. For the moment
only Mysql is supported. This code is __imported__ from Broker.
The managed_brok function is called by Broker for manage the broks. It calls
the manage_*_brok functions that create queries, and then run queries.
"""
def __init__(self, conf):
BaseModule.__init__(self, conf)
# Mapping for name of data and transform function
self.mapping = {
'program_status': {
'program_start': {'name': 'program_start_time', 'transform': de_unixify},
'pid': {'name': 'process_id', 'transform': None},
'last_alive': {'name': 'status_update_time', 'transform': de_unixify},
'is_running': {'name': 'is_currently_running', 'transform': None},
'last_log_rotation': {'name': 'last_log_rotation', 'transform': de_unixify},
'last_command_check': {'name': 'last_command_check', 'transform': de_unixify}
},
}
self.host = conf.host
self.user = conf.user
self.password = conf.password
self.database = conf.database
self.character_set = conf.character_set
self.port = int(getattr(conf, 'port', '3306'))
self.prefix = getattr(conf, 'prefix', 'nagios_')
# Centreon ndo add some fields like long_output
# that are not in the vanilla ndo
self.centreon_version = False
self.synchronize_database_id = int(conf.synchronize_database_id)
# Called by Broker so we can do init stuff
# TODO: add conf param to get pass with init
# Conf from arbiter!
def init(self):
logger.info("I connect to NDO database")
self.db = DBMysql(self.host, self.user, self.password, self.database,
self.character_set, table_prefix=self.prefix,
port=self.port)
self.connect_database()
# Cache for hosts and services
# The structure is as follow:
# First the instance id then the host / (host,service desc)
# to access the wanted data
self.services_cache_sync = {}
self.hosts_cache_sync = {}
# We need to search for centreon_specific fields, like long_output
query = u"select TABLE_NAME from information_schema.columns " \
"where TABLE_SCHEMA='ndo' and " \
"TABLE_NAME='%sservicestatus' and " \
"COLUMN_NAME='long_output';" % self.prefix
self.db.execute_query(query)
row = self.db.fetchone()
if row is None or len(row) < 1:
self.centreon_version = False
else:
self.centreon_version = True
logger.info("[MySQL/NDO] Using the centreon version")
# Cache for database id
# In order not to query the database every time
self.database_id_cache = {}
# Mapping service_id in Shinken and in database
# Because can't acces host_name from a service everytime :(
self.mapping_service_id = {}
# Todo list to manage brok
self.todo = {}
# Get a brok, parse it, and put in in database
# We call functions like manage_ TYPEOFBROK _brok that return us queries
def manage_brok(self, b):
# We need to do some brok mod, so we copy it
new_b = copy.deepcopy(b)
# If we synchronize, must look for id change
if self.synchronize_database_id != 0 and 'instance_id' in new_b.data:
# If we use database sync, we have to synchronize database id
# so we wait for the instance name
brok_id = new_b.data['instance_id']
converted_instance_id = self.convert_id(brok_id)
if converted_instance_id is not None:
new_b.data['instance_id'] = converted_instance_id
queries = BaseModule.manage_brok(self, new_b)
if queries is not None:
for q in queries:
self.db.execute_query(q)
if converted_instance_id is None:
if brok_id in self.todo:
#.........这里部分代码省略.........
示例8: Glpidb_broker
class Glpidb_broker(BaseModule):
def __init__(self, modconf, host=None, user=None, password=None, database=None, character_set=None, database_path=None):
#Mapping for name of data, rename attributes and transform function
self.mapping = {
#Host
'host_check_result' : {
'plugin_monitoring_services_id' : {'transform' : None},
'event' : {'transform' : None},
'perf_data' : {'transform' : None},
'output' : {'transform' : None},
'state' : {'transform' : None},
'latency' : {'transform' : None},
'execution_time' : {'transform' : None},
'state_type' : {'transform' : None},
},
#Service
'service_check_result' : {
'plugin_monitoring_services_id' : {'transform' : None},
'plugin_monitoring_servicescatalogs_id' : {'transform' : None},
'event' : {'transform' : None},
'perf_data' : {'transform' : None},
'output' : {'transform' : None},
'state' : {'transform' : None},
'latency' : {'transform' : None},
'execution_time' : {'transform' : None},
'state_type' : {'transform' : None},
}
}
# Last state of check
# self.checkstatus = {
# '0' : None,
# }
BaseModule.__init__(self, modconf)
self.host = host
self.user = user
self.password = password
self.database = database
self.character_set = character_set
self.database_path = database_path
from shinken.db_mysql import DBMysql
print "Creating a mysql backend"
self.db_backend = DBMysql(host, user, password, database, character_set)
#Called by Broker so we can do init stuff
#TODO : add conf param to get pass with init
#Conf from arbiter!
def init(self):
print "I connect to Glpi database"
self.db_backend.connect_database()
def preprocess(self, type, brok, checkst):
new_brok = copy.deepcopy(brok)
#Only preprocess if we can apply a mapping
if type in self.mapping:
#print "brok data : ", brok.data
try:
s = brok.data['service_description'].split('-')
try:
if 'businessrules' in s[2]:
new_brok.data['plugin_monitoring_servicescatalogs_id'] = s[1]
except:
new_brok.data['plugin_monitoring_services_id'] = s[1]
new_brok.data['event'] = brok.data['output']
except:
try:
s = brok.data['host_name'].split('-')
new_brok.data['plugin_monitoring_services_id'] = s[1]
new_brok.data['event'] = brok.data['output']
except:
pass
to_del = []
to_add = []
mapping = self.mapping[brok.type]
for prop in new_brok.data:
#ex : 'name' : 'program_start_time', 'transform'
if prop in mapping:
#print "Got a prop to change", prop
val = new_brok.data[prop]
if mapping[prop]['transform'] is not None:
print "Call function for", type, prop
f = mapping[prop]['transform']
val = f(val)
name = prop
if 'name' in mapping[prop]:
name = mapping[prop]['name']
to_add.append((name, val))
to_del.append(prop)
else:
to_del.append(prop)
for prop in to_del:
del new_brok.data[prop]
for (name, val) in to_add:
new_brok.data[name] = val
else:
#.........这里部分代码省略.........
示例9: create_db
def create_db(self):
self.db = DBMysql(host='localhost', user='root', password='root', database='merlin', character_set='utf8')
示例10: Ndodb_Mysql_broker
class Ndodb_Mysql_broker(BaseModule):
def __init__(self, conf):
BaseModule.__init__(self, conf)
# Mapping for name of data and transform function
self.mapping = {
'program_status' : {
'program_start' : {'name' : 'program_start_time', 'transform' : de_unixify},
'pid' : {'name' : 'process_id', 'transform' : None},
'last_alive' : {'name' : 'status_update_time', 'transform' : de_unixify},
'is_running' : {'name' : 'is_currently_running', 'transform' : None}
},
}
self.host = conf.host
self.user = conf.user
self.password = conf.password
self.database = conf.database
self.character_set = conf.character_set
self.port = int(getattr(conf, 'port', '3306'))
# Centreon ndo add some fields like long_output that are not in the vanilla ndo
self.centreon_version = False
self.synchronise_database_id = int(conf.synchronise_database_id)
# Called by Broker so we can do init stuff
# TODO : add conf param to get pass with init
# Conf from arbiter!
def init(self):
logger.log("I connect to NDO database")
self.db = DBMysql(self.host, self.user, self.password, self.database,
self.character_set, table_prefix='nagios_', port=self.port)
self.connect_database()
# Cache for hosts and services
# The structure is as follow:
# First the instance id then the host / (host,service desc) to access the wanted data
self.services_cache_sync = {}
self.hosts_cache_sync = {}
# We need to search for centreon_specific fields, like long_output
query = u"select TABLE_NAME from information_schema.columns where TABLE_SCHEMA='ndo' and TABLE_NAME='nagios_servicestatus' and COLUMN_NAME='long_output';"
self.db.execute_query(query)
row = self.db.fetchone()
if row is None or len(row) < 1:
self.centreon_version = False
else:
self.centreon_version = True
logger.log("[MySQL/NDO] Using the centreon version")
# Cache for database id
# In order not to query the database every time
self.database_id_cache = {}
# Mapping service_id in Shinken and in database
# Because can't acces host_name from a service everytime :(
self.mapping_service_id = {}
# Todo list to manage brok
self.todo = []
# Get a brok, parse it, and put in in database
# We call functions like manage_ TYPEOFBROK _brok that return us queries
def manage_brok(self, b):
# We need to do some brok mod, so we copy it
new_b = copy.deepcopy(b)
# If we syncronize, must look for id change
if self.synchronise_database_id != '0' and 'instance_id' in new_b.data:
# If we use database sync, we have to synchronise database id
# so we wait for the instance name
if 'instance_name' not in new_b.data :
self.todo.append(new_b)
return
# We convert the id to write properly in the base using the
# instance_name to reuse the instance_id in the base.
else:
new_b.data['instance_id'] = self.convert_id(new_b.data['instance_id'], new_b.data['instance_name'])
self.todo.append(new_b)
for brok in self.todo :
# We have to put the good instance ID to all brok waiting
# in the list then execute the query
brok.data['instance_id'] = new_b.data['instance_id']
queries = BaseModule.manage_brok(self, brok)
if queries is not None:
for q in queries :
self.db.execute_query(q)
# We've finished to manage the todo, so we empty it
self.todo = []
return
# Executed if we don't synchronise or there is no instance_id
queries = BaseModule.manage_brok(self,new_b)
if queries is not None:
for q in queries :
self.db.execute_query(q)
return
#.........这里部分代码省略.........
示例11: Glpidb_broker
class Glpidb_broker(BaseModule):
def __init__(self, modconf):
BaseModule.__init__(self, modconf)
self.hosts_cache = {}
self.services_cache = {}
# Database configuration
self.host = getattr(modconf, 'host', '127.0.0.1')
self.user = getattr(modconf, 'user', 'shinken')
self.password = getattr(modconf, 'password', 'shinken')
self.database = getattr(modconf, 'database', 'glpidb')
self.character_set = getattr(modconf, 'character_set', 'utf8')
logger.info("[glpidb] using '%s' database on %s (user = %s)", self.database, self.host, self.user)
# Database tables update configuration
self.update_availability = bool(getattr(modconf, 'update_availability', '0')=='1')
self.update_shinken_state = bool(getattr(modconf, 'update_shinken_state', '0')=='1')
self.update_services_events = bool(getattr(modconf, 'update_services_events', '0')=='1')
self.update_hosts = bool(getattr(modconf, 'update_hosts', '0')=='1')
self.update_services = bool(getattr(modconf, 'update_services', '0')=='1')
self.update_acknowledges = bool(getattr(modconf, 'update_acknowledges', '0')=='1')
logger.info("[glpidb] updating availability: %s", self.update_availability)
logger.info("[glpidb] updating Shinken state: %s", self.update_shinken_state)
logger.info("[glpidb] updating services events: %s", self.update_services_events)
logger.info("[glpidb] updating hosts states: %s", self.update_hosts)
logger.info("[glpidb] updating services states: %s", self.update_services)
logger.info("[glpidb] updating acknowledges states: %s", self.update_acknowledges)
def init(self):
from shinken.db_mysql import DBMysql
logger.info("[glpidb] Creating a mysql backend : %s (%s)" % (self.host, self.database))
self.db_backend = DBMysql(self.host, self.user, self.password, self.database, self.character_set)
logger.info("[glpidb] Connecting to database ...")
self.db_backend.connect_database()
logger.info("[glpidb] Connected")
# Get a brok, parse it, and put in in database
def manage_brok(self, b):
# Build initial host state cache
if b.type == 'initial_host_status':
host_name = b.data['host_name']
logger.debug("[glpidb] initial host status : %s", host_name)
try:
logger.debug("[glpidb] initial host status : %s : %s", host_name, b.data['customs'])
self.hosts_cache[host_name] = {'hostsid': b.data['customs']['_HOSTID'], 'itemtype': b.data['customs']['_ITEMTYPE'], 'items_id': b.data['customs']['_ITEMSID'] }
except:
self.hosts_cache[host_name] = {'items_id': None}
logger.debug("[glpidb] no custom _HOSTID and/or _ITEMTYPE and/or _ITEMSID for %s", host_name)
logger.debug("[glpidb] initial host status : %s is %s", host_name, self.hosts_cache[host_name]['items_id'])
# Build initial service state cache
if b.type == 'initial_service_status':
host_name = b.data['host_name']
service_description = b.data['service_description']
service_id = host_name+"/"+service_description
logger.debug("[glpidb] initial service status : %s", service_id)
if not host_name in self.hosts_cache or self.hosts_cache[host_name]['items_id'] is None:
logger.debug("[glpidb] initial service status, host is not defined in Glpi : %s.", host_name)
return
try:
logger.debug("[glpidb] initial service status : %s : %s", service_id, b.data['customs'])
self.services_cache[service_id] = {'itemtype': b.data['customs']['_ITEMTYPE'], 'items_id': b.data['customs']['_ITEMSID'] }
except:
self.services_cache[service_id] = {'items_id': None}
logger.debug("[glpidb] no custom _ITEMTYPE and/or _ITEMSID for %s", service_id)
logger.debug("[glpidb] initial service status : %s is %s", service_id, self.services_cache[service_id]['items_id'])
# Manage host check result if host is defined in Glpi DB
if b.type == 'host_check_result':
host_name = b.data['host_name']
logger.debug("[glpidb] host check result: %s: %s", host_name, b.data)
# Update Shinken state table
if self.update_shinken_state:
self.record_shinken_state(host_name, '', b)
# Update availability
if self.update_availability:
self.record_availability(host_name, '', b)
if host_name in self.hosts_cache and self.hosts_cache[host_name]['items_id'] is not None:
start = time.time()
self.record_host_check_result(b)
logger.debug("[glpidb] host check result: %s, %d seconds", host_name, time.time() - start)
# Manage service check result if service is defined in Glpi DB
if b.type == 'service_check_result':
host_name = b.data['host_name']
service_description = b.data['service_description']
service_id = host_name+"/"+service_description
logger.debug("[glpidb] service check result: %s", service_id)
# Update Shinken state table
#.........这里部分代码省略.........