def mysql_query_per_second(self): try: status = {} query = "show status like 'Queries';" cur = self.__conx.cursor(cursor_class=MySQLCursorDict) cur.execute(query) qt1 = 0 #qt2 = 0 for row in cur.fetchall(): qt1 = long(row['Value']) cur.close() #time.sleep(01) #cur = self.__conx.cursor(cursor_class=MySQLCursorDict) #cur.execute(query) #for row in cur.fetchall(): # qt2 = long(row['Value']) #cur.close() #status['qps'] = qt2 - qt1 status['qps'] = qt1 return status except Exception, e: log.debug("Error %s on execute %s" % (e, query))
def call_invoked(): while True: # if reload log.debug("Thread Invoked Started") collector = Collector() collector.Invoked() time.sleep(long(invoked_sleep_time)) # Load module - dpcollector.conf or CONSTANT
def pgsql_connect(self): try: self.__conn__ = psycopg2.connect(database=self.database, user=self.user, password=self.password, host=self.host, port=self.port) except psycopg2.DatabaseError, e: log.debug("Error on connect database: %s" % (e)) log.debug("Error on MySQL with user %s host %s, database %s and socket %s" % ( self.user, str(self.host), str(self.database), str(self.socket)))
def toBuffer(self, line): log.debug("Sending data do Buffer") size = sys.getsizeof(self.queue) - 624 # 624 is a deque size when empty if self.max_buffer_size: if size > long(self.max_buffer_size): log.debug("Pyabbix flush to disk...buffer full: " + str(size)) self.toDisk(self.queue) else: self.queue.append(line)
def pgsql_ping(self): try: cur = self.__conn__.cursor() cur.execute("select 1") row = cur.fetchone() cur.close() return 1 except psycopg2.DatabaseError, e: log.debug("PostgreSQL ping fail") return 0
def mysql_execute(self, query): try: self.__connect(self.user, self.password, self.host, self.database, self.socket) cur = self.__conx.cursor() cur.execute(query) return cur except: log.debug("Error on execute %s" % query)
def mysql_slave_status(self): try: query = 'show slave status;' cur = self.__conx.cursor(cursor_class=MySQLCursorDict) cur.execute(query) for row in cur.fetchall(): pass cur.close() return row except Exception, e: log.debug("Error %s on execute %s" % (e, query))
def mysql_show_variables(self): try: status = {} query = 'show variables;' cur = self.__conx.cursor(cursor_class=MySQLCursorDict) cur.execute(query) for row in cur.fetchall(): status[row['Variable_name'].lower()] = row['Value'] cur.close() return status except Exception, e: log.debug("Error %s on execute %s" % (e, query))
def get_folder_size(self, folder): ''' Return in K ''' command = 'du -sc -b ' + folder try: du_info = os.popen(command).read() for line in du_info.split("\n"): if "total" in line: line = line.strip().split() return line[0] except Exception, e: log.debug("Error on get_folder_size: %s" % e) return 0
def get_pid_memory_info(self, pid=None, pid_file=None): status = {} try: if pid_file != None and pid == None: command = "cat %s" % pid_file result = os.popen(command).read() for line in result.split(): pass pid = line except Exception, e: log.debug("Error on get pid by pid_file: %s - %s" % (pid_file, e)) pass
def toWrite(self, key, value, timestamp=None): if timestamp == None: timestamp = str(self.ts) try: key = CONS_DOMAIN + key except: pass line = "%s %s %s %s" % (self.host_name, key, timestamp, value) log.debug("Write line %s" % line) if self.buffered: self.toBuffer(line) else: self.toDisk(line)
def mysql_connect(self): try: if self.socket: self.__conx = mysql.connector.connect(user=self.user, password=self.password, host=self.host, unix_socket=self.socket, database='mysql', raw=self.raw_value, connection_timeout=MYSQL_CONNECTION_TIMEOUT) else: self.__conx = mysql.connector.connect(user=self.user, password=self.password, host=self.host, database='mysql', raw=self.raw_value, connection_timeout=MYSQL_CONNECTION_TIMEOUT) except Exception, e: log.debug("Error on connect database: %s" % (e)) log.debug("Error on MySQL with user %s host %s, database %s and socket %s" % ( self.user, str(self.host), str(self.database), str(self.socket)))
def check_host_bigip_pool(self): os_get = MetaOS() _local_ip_string = os_get.get_network_ip_byiface(os_get.get_network_default_gateway_iface()) #_local_ip_string = '10.80.40.13' _local_ip_adrress_long = ip2long(_local_ip_string) log.debug("BigIP: Start query in pools... this very expensive...") for poolname in self.bigip.getAllPools(): result_member = self.bigip.getAllMembersOfPool(poolname.strip()) for row_host in result_member.keys(): result_ip_address_long = ip2long(str(row_host)) if _local_ip_adrress_long == result_ip_address_long: self.exist_in_pool = True log.debug("BigIP: This server(%s) has found in %s" % (_local_ip_string, poolname)) avail = result_member.get(row_host).get('avail') enabled = result_member.get(row_host).get('enabled') cons_a = CONS_BIGIP_AVAIL.get(1) cons_b = CONS_BIGIP_ENABLE.get(1) if avail == cons_a and enabled == cons_b: self.enabled_in_poll = True log.debug( "BigIP: This server(%s) has found in and Enabled :-) %s" % (_local_ip_string, poolname)) break else: # Break internal loop for row_host log.debug( "BigIP: This server(%s) has found in %s but unavailable :-(" % (_local_ip_string, poolname)) else: continue if self.enabled_in_poll: break
def send(self, mydata): socket.setdefaulttimeout(5) data_length = len(mydata) data_header = struct.pack("i", data_length) + "\0\0\0\0" data_to_send = self.header % (data_header, mydata) try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.zserver, self.zport)) sock.send(data_to_send) except Exception as err: log.debug("Zabbix Send: Error talking to server: %s\n" % err) return (255, err) try: response_header = sock.recv(5) if not response_header == "ZBXD\1": log.debug("Zabbix Send: Invalid response from server. Malformed data?\n---\n%s\n---\n" % mydata) except Exception as err: return (254, err) response_data_header = sock.recv(8) response_data_header = response_data_header[:4] response_len = struct.unpack("i", response_data_header)[0] response_raw = sock.recv(response_len) sock.close() response = json.loads(response_raw) match = re.match("^.*Failed\s(\d+)\s.*$", str(response)) if match is None: log.debug("Unable to parse server response - " + "\n%s\n" % response) else: fails = int(match.group(1)) if fails > 0: log.debug("Zabbix Send: Failures reported by zabbix when sending:" + "\n%s\n" % mydata) return (1, response) return (0, response)
def mysql_show_engine_innodb_status(self): ''' Use MySQL_Utils ''' rs = None try: query = 'show engine innodb status' cur = self.__conx.cursor() cur.execute(query) # MySQL 5.1 < fechone() is good but not work in 5.5 > for row in cur.fetchall(): pass cur.close() retorno = parse_innodb_status(row, self.mysql_version_tuple()) return retorno except Exception, e: log.debug("Error %s on execute %s" % (e, query))
def toDisk(self, my_object): try: log.debug("==== Write data to disk =====") if type(my_object) == type(str()) or type(unicode()): log.debug("Object type str - Continue with simple mode") with open(self.filename, "a") as f: f.write(my_object + "\n") log.debug("Write: flush to disk...You write %s (%d bytes)." % (self.filename, len(my_object))) else: while my_object: with open(self.filename, "a") as f: f.write(my_object.popleft()) log.debug("Write: flush to disk %s (%d bytes)." % (self.filename, len(my_object))) except (OSError, IOError), e: log.debug("Error: Write data to disk: " + str(e))
def mysql_show_database_usage_status(self): status = {} if self.mysql_version_tuple() >= (5, 1, 0): try: query = 'SELECT COUNT(DISTINCT(table_schema)) as num_db, CONCAT(sum(ROUND((CAST(DATA_LENGTH + INDEX_LENGTH AS SIGNED) - CAST(DATA_FREE AS SIGNED)),0))) AS size FROM INFORMATION_SCHEMA.TABLES;' cursor = self.__conx.cursor(raw=True) cursor.execute(query) for row in cursor.fetchall(): status['number_database'] = row[0] status['total_data'] = row[1] except Exception, e: log.debug("Error %s on execute %s" % (e, query)) status['number_database'] = 0 status['total_data'] = 0 if not self.mysql_isconnected(): self.mysql_connect()
def toZabbixNative(self): try: file_size = os.path.getsize(self.filename) z = ZabbixSend(server=str(zabbix_server_host).strip()) try: data_file = open(self.filename, "r") for row in data_file: # localhost mysql.show_status.created_tmp_tables 1383845532 1 # host, key, value, evt_time=None host = row.split(' ')[0].strip() key = row.split(' ')[1].strip() evt_time = row.split(' ')[2].strip() value = row.split(' ')[3].strip() z.add_data(host, key, value, evt_time) data_file.close() z.send(z.build_all()) self.clearfile() except (IOError, Exception), e: log.debug("Error on send data to Zabbix (Native) - error: " + str(e)) return True
def clearfile(self): try: if CONS_CLEAR_FILE_AFTER_TRAP: os.remove(self.filename) log.debug("File " + self.filename + " removed after zabbix_send") else: log.debug("Delete file cache is disabled in source code: CONS_CLEAR_FILE_AFTER_TRAP = False") except IOError, ioe: log.debug("Error on remove file: " + str(ioe))
def Invoked(self): log.debug("Collector - OS - Base - Invoked Thread") get_os = GetOS(CONS_COLLECT_FOR["INVOKED"]) get_os.collect() log.debug("Collector - DB - Base - Invoked Thread") get_db = GetDB(CONS_COLLECT_FOR["INVOKED"]) get_db.collect() log.debug("Collector - Net - Base - Invoked Thread") get_net = GetNet(CONS_COLLECT_FOR["INVOKED"]) get_net.collect() self.sendData(CACHE_FILE_INVOKED)
def Continuous(self): log.debug("Collector - DB - Base - Continuos Thread") get_db = GetDB(CONS_COLLECT_FOR["CONTINUOUS"]) get_db.collect() log.debug("Collector - OS - Base - Continuos Thread") get_os = GetOS(CONS_COLLECT_FOR["CONTINUOUS"]) get_os.collect() log.debug("Collector - Net - Base - Continuos Thread") get_net = GetNet(CONS_COLLECT_FOR["CONTINUOUS"]) get_net.collect() #Send data self.sendData(CACHE_FILE_CONTINUOUS) pass #from lib.collector.OScollector_CRITICAL import * #from lib.collector.DBcollector_CRITICAL import *
def write_key(self, key, value, ts=str(int(time.time()))): log.debug("get: %s" % key) self.wcache.toWrite(key, value, ts)
def call_continuous(): while True: log.debug("Thread Continuous Started") collector = Collector() collector.Continuous() time.sleep(long(continuos_sleep_time)) # Load module - dpcollector.conf or CONSTANT
def reload(self): log.debug("Reload Signal - Thread by external call starting") call_invoked()
def mysql_isconnected(self): try: return self.__conx.is_connected() except: return False log.debug("Error on get mysql test connect")
def toZabbix(self): try: file_size = os.path.getsize(self.filename) cmd_send = "%s -z %s -s %s -T -i %s" % ( str(zabbix_sender).strip(), str(zabbix_server_host).strip(), str(host_name).strip(), str(self.filename).strip()) try: process = subprocess.Popen(cmd_send.strip(), shell=True, stdout=subprocess.PIPE) stdout = process.communicate()[0] process.wait() if sys.version_info >= (2, 7): log.debug("Sending %s - %s " % (self.filename, sizeof_fmt(file_size))) log.debug("Zabbix Server command: %s" % cmd_send) log.debug("File %s - sended by zabbix_sender %s: " % (str(self.filename), str(stdout))) else: log.debug("Sending %s" % (self.filename)) log.debug("Zabbix Server command: %s" % cmd_send) log.debug("File %s - sended by zabbix_sender %s: " % (str(self.filename), str(stdout))) except (IOError, Exception), e: log.debug("Error on zabbix_send - command " + cmd_send + " error: " + str(e)) self.clearfile() return True
def mysql_get_timezone(self): try: return self.__conx.get_time_zone() except: log.debug("Error on get timezone")
stdout = process.communicate()[0] process.wait() if sys.version_info >= (2, 7): log.debug("Sending %s - %s " % (self.filename, sizeof_fmt(file_size))) log.debug("Zabbix Server command: %s" % cmd_send) log.debug("File %s - sended by zabbix_sender %s: " % (str(self.filename), str(stdout))) else: log.debug("Sending %s" % (self.filename)) log.debug("Zabbix Server command: %s" % cmd_send) log.debug("File %s - sended by zabbix_sender %s: " % (str(self.filename), str(stdout))) except (IOError, Exception), e: log.debug("Error on zabbix_send - command " + cmd_send + " error: " + str(e)) self.clearfile() return True except: log.debug("File %s is empty... I don't need send" % self.filename) return False pass def toZabbixNative(self): try: file_size = os.path.getsize(self.filename) z = ZabbixSend(server=str(zabbix_server_host).strip()) try: data_file = open(self.filename, "r") for row in data_file: # localhost mysql.show_status.created_tmp_tables 1383845532 1 # host, key, value, evt_time=None host = row.split(' ')[0].strip() key = row.split(' ')[1].strip()
def mysql_version_string(self): try: return self.__conx.get_server_info() except: log.debug("Error on get version")
def mysql_version_tuple(self): try: return self.__conx.get_server_version() except: log.debug("Error on get version")