#!/usr/bin/env python
# encoding: utf-8


from classes.dealOracle import *
from classes.dealSql import *
from message import *
import os, sys, datetime, time


if __name__ == "__main__":
    try:
        querySql = "select t.res_log_id,t.err_content,t.mot_msg,t.err_value,t.insert_time,t.role_name from monitor.vop_mot_res_log t where t.insert_time between sysdate -3 and sysdate and t.res_flag ='0'"
        # 获取vop_mot_res_log res_flag为0 么有发送的信息
        filename = "./config/dbinfo.ini"
        db = dealOracle()
        dblist = db.getConnectInfo(filename)
        dbconn = db.connectDb(dblist[0])  # 链接数据库
        cursor = db.getCursor(dbconn)
        db.executeSQL(cursor, querySql)
        result = db.getSQLrst(cursor)  # 获取结果
        idList = []  # 定义res_log_id 的列表[res_log_id01,res_log_id02,res_log_id03,....],以便跟新res_flag 的状态
        msgDic = {}  # 定 义要发送信息的字典,以告警角色为key值
        roleList = []
        for res in result:  # 遍历 从数据库获取的信息
            id = res[0]
            idList.append(id)  # 获得res_log_id 列表
            errMessage = str(res[4]) + "," + res[1] + "," + res[2] + ":" + str(res[3])  # 生成要发送的短信内容
            role = res[5]  # 获取告警项角色,以便发送信息
            if role in msgDic.keys():
                msgDic[role].append(errMessage)
from classes.dealSsh import *
from classes.dealData import *
from classes.dealOracle import *
from classes.dealSql import *
import os,sys,datetime,time

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
#设置环境变量,防止查询出现乱码
now=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
#执行脚本时的时间

if __name__ == '__main__':
    try:
        infoSql="select h.host_ip,h.username,h.password,h.port,c.cmd_id,c.mot_cmd,d.mod_name,h.host_info,d.mod_msg,d.mod_remark,c.mot_msg,c.mot_cmd_type,c.mot_relation,c.mot_value,c.mot_rate,c.exec_time from monitor.vop_host_conf h, monitor.vop_mot_cmd_conf c,monitor.vop_mot_mod_conf d where c.mot_flag = 1 and h.h_flag = 1 and h.h_id = c.host_id and c.mod_id=d.mod_id"
        #从数据库 获取主机信息,监控命令,监控类型的sql
        db=dealOracle() #实例化处理oracle数据库的类
        filename='./config/dbinfo.ini' #链接数据库的配置文件
        dblist=db.getConnectInfo(filename)
        dbconn=db.connectDb(dblist[0]) #链接数据库
        cursor=db.getCursor(dbconn) #获取游标
        db.executeSQL(cursor,infoSql) #执行sql
        result=db.getSQLrst(cursor) #获取sql执行结果
        time.sleep(2)
        deal=dealdbInfo() #实例化处理信息的类,根据命令执行的频率,判断那条命令应该执行
        infoDic=deal.dealInfo(result)#获取处理后的命令的结果
        ssh=getData() #实例化ssh 登录主机获取主机监控信息的类
        data=ssh.sshMain(infoDic)#获取主机的监控信息
        id=tuple(data[1]) #获取执行的命令的id,用来更新vop_mot_cmd_conf 表的命令执行时间exce_time
        result=data[0] #获取执行命令的结果
        dealSql=dealSql()#实例化处理sql的类
        updateSql=dealSql.upCmdIdSql(id)#获取更新vop_mot_cmd_conf 表exce_time 的sql