def check_CPU(self): """检查CPU""" ssh = ssh_server(self.host_obj) query_result = fun_query( ssh, """vmstat|awk 'NR==3 {print ($13+$14)/100}'""", """vmstat""") data = api.format_json(self.project_nick, self.host_obj.host_nick, None, self.service_dict['m_type'], self.service_dict['m_dim'], query_result[0], query_result[1], query_result[2]) api.urlPost(data)
def check_process(self): """检查newbi进程""" ssh = ssh_server(self.host_obj) query_result = fun_query( ssh, """ps -ef | grep jetty | grep -v "grep" | wc -l""", """ps -ef | grep jetty | grep -v 'grep'""") data = api.format_json(self.project_nick, self.host_obj.host_nick, None, self.service_dict['m_type'], self.service_dict['m_dim'], query_result[0], query_result[1], query_result[2]) api.urlPost(data)
def check_memory(self): """检查MEMORY""" ssh = ssh_server(self.host_obj) query_result = fun_query(ssh, """vmstat|awk 'NR==3 {print $4/1024"MB"}'""", """vmstat""") data = api.format_json(self.project_nick, self.host_obj.host_nick, None, self.service_dict['m_type'], self.service_dict['m_dim'], query_result[0], query_result[1], query_result[2]) api.urlPost(data)
def check_disk(self): """检查DISK""" ssh = ssh_server(self.host_obj) query_result = fun_query( ssh, """iostat -dx|awk 'BEGIN{max=0} {if($14+0>max+0) max=$14} END{print max/100}'""", """iostat""") data = api.format_json(self.project_nick, self.host_obj.host_nick, None, self.service_dict['m_type'], self.service_dict['m_dim'], query_result[0], query_result[1], query_result[2]) api.urlPost(data)
def check_IOPS(self): """检查IOPS""" ssh = ssh_server(self.host_obj) # 最大的一个iops query_result = fun_query( ssh, """iostat |awk 'BEGIN{max=0} NR>6 {if($2+0>max+0) max=$2} END{print max}'""", """iostat""") data = api.format_json(self.project_nick, self.host_obj.host_nick, None, self.service_dict['m_type'], self.service_dict['m_dim'], query_result[0], query_result[1], query_result[2]) api.urlPost(data)
def db_fun_query(self, query_m_value, query_m_log): """ 执行数据库系统表查询 :param query_m_value: 查询监控值的sql语句 :param query_m_log: 查询监控日志的sql语句 :return: 返回查询结果 """ conn = db_connection(self.db_object) try: cur = conn.cursor() except Exception as e: conn.close() raise ValueError("ERROR 数据库连接异常:" + str(e)) try: m_timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # m_value cur.execute(query_m_value) m_value = cur.fetchone()[0] # m_log print query_m_log cur.execute(query_m_log) # 对数据做处理 query2_result_org = cur.fetchone() # 乳沟查询数据为空,记录查询语句 if query2_result_org is None: m_log = query_m_log else: query2_result = list(query2_result_org) # 将查询结果连接起来,方便日志记录 # eleminate None value query2_result = [ str(x) for x in query2_result if x is not None ] m_log = " ".join(query2_result) data = api.format_json(self.project_nick, self.host_obj.host_nick, self.db_object.db_nick, self.service_dict['m_type'], self.service_dict['m_dim'], m_value, m_log, m_timestamp) return data except Exception as e: raise ValueError("ERROR 数据库操作异常:" + str(e)) finally: """确保connection关闭""" cur.close() conn.close()
def check_process(self): """ 检查KETTLE进程是否存在 1. 创建ssh 对象 2. 在目标服务器上执行命令 3. post数据 :return: """ # 1. 生成ssh对象 ssh = ssh_server(self.host_obj) # 2. 查询数据 query_result = fun_query( ssh, """ps -ef | grep spoon.sh | grep -v 'grep'|wc -l""", """ps -ef | grep spoon.sh | grep -v 'grep'""") # 3. 格式化数据 data = api.format_json(self.project_nick, self.host_obj.host_nick, None, self.service_dict['m_type'], self.service_dict['m_dim'], query_result[0], query_result[1], query_result[2]) # 4. 向接口中post数据 api.urlPost(data)