#!/usr/local/python2.7/bin/python
# -*- coding=utf-8 -*-

import redis_connector as redis
import json,time
import urllib2
import MySQLdb
import new

warning_list = []
critical_list = []
well_list = []
host_name = {}
well_temp_list = []
critical_temp_list = []
operators = {'1':'gt','2':'lt','3':'eq'}
def fetch_monitored_list(host_name):
    monitor_list = []
    service = {}       
    for host_id in host_name:
        #url_str = 'http://10.73.143.197:9000/api/configuration/1/?format=json'
        url_str = "http://192.168.2.103:9000/api/configuration/"+host_id+"/?format=json"
        req = urllib2.urlopen(url_str)
        result = req.read()
        configs= json.loads(result)
        #在下面的for循环中如果status_monitor_on为false的话，将不会监控
        if configs.get('status_monitor_on') is True:
            for template in configs.get('template_list'):
                monitor_list +=template['service_list']
                #graph_list = configs.get('template_list')['graph_list']
                #print graph_list
                
         
        for i in range(len(monitor_list)):
            #print str(monitor_list[i]['conditons'][0]['name']).split('.')[-1]
            #取页面中的监控项名
            #print monitor_list
            key = host_name[host_id]+'::'+ str(monitor_list[i]['conditons'][0]['name']).split('.')[-1]
            #取redis中的key
            redis_key = host_name[host_id]+'::'+monitor_list[i]['service']['name']
            #取监控项的阀值
            service[key] = str(monitor_list[i]['conditons'][0]['threshold']).split('.')[-1]
            #取"name":"LinuxTemplate.linux.salt.salt_pg"中salt_pg
            zhulh = str(monitor_list[i]['conditons'][0]['name']).split('.')[-1]
            #取"name":"LinuxTemplate.linux.salt.salt_pg"中salt
            service_name = str(str(monitor_list[i]['conditons'][0]['name']).split('.')[2:-1]).strip("['']")
            #print len(str(monitor_list[i]['conditons'][0]['name']).split('.')),str(monitor_list[i]['conditons'][0]['name']).split('.')[2:-1]
            #取redis中的数据
            service_data = redis.r.get(redis_key)
            #取运算符            
            operators_num = str(monitor_list[i]['conditons'][0]['operator'])
            
            if service_data is not None:
                service_data = json.loads(service_data)
                time_pass_since_last_recv = time.time() - float(service_data['time_stamp'])
                interval = int(monitor_list[i]['check_interval'])
                new_zhulh = host_name[host_id]+':'+service_name+':'+zhulh+':'+eval(service_data['data'])[zhulh]
                #if time_pass_since_last_recv >= interval + 10:
                if time_pass_since_last_recv > 4*interval:
                    print "\033[41;1mService %s has no data for %ss\033[0m" %(zhulh,(time_pass_since_last_recv))                       
                    if new_zhulh not in critical_list:
                        critical_list.append(new_zhulh)
                        if new_zhulh in well_list:                                        
                            well_list.remove(new_zhulh)
                    print critical_list
                else:
                    #if time.time() >= interval + float(service_data['time_stamp']) -10: 
                    if eval(service_data['data'])['status'] == 0:
                        if operators[operators_num] == 'gt':
                            '''
                            #在这里eval是将service_data['data']字符串转换成dict
                            #eval(service_data['data'])[zhulh]
                            '''
                            if float(service[key]) > float(eval(service_data['data'])[zhulh]):
                                print "\033[32;1mService %s crossed OK ,current val is %s\033[0m" %(zhulh,eval(service_data['data'])[zhulh])
                                W_to_list(new_zhulh)
                                '''
                                if new_zhulh not in warning_list or new_zhulh not in critical_list:
                                    if new_zhulh not in well_list:
                                        well_temp_list.append(new_zhulh)
                                        print well_temp_list
                                    #if zhulh not in well_list and cross_well_count >= 3:
                                    if new_zhulh not in well_list and well_temp_list.count(new_zhulh) >= 3:
                                        for i in range(well_temp_list.count(new_zhulh)):
                                            well_temp_list.remove(new_zhulh)
                                        well_list.append(new_zhulh)
                                        if new_zhulh in warning_list:
                                            warning_list.remove(new_zhulh)
                                        if new_zhulh in critical_list:                                    
                                            critical_list.remove(new_zhulh)
                                            '''
                            else:
                                C_to_list(new_zhulh)
                                '''
                                if new_zhulh in critical_list:
                                    for i in range(critical_temp_list.count(new_zhulh)):
                                            critical_temp_list.remove(new_zhulh)
                                else:
                                    critical_temp_list.append(new_zhulh)
                                    if critical_temp_list.count(new_zhulh) >= 3:                                        
                                        critical_list.append(new_zhulh)
                                        if new_zhulh in well_list:                                        
                                            well_list.remove(new_zhulh)
                                            '''
                        elif operators[operators_num] == 'lt':
                            if float(service[key]) < float(eval(service_data['data'])[zhulh]):
                                print "\033[32;1mService %s crossed OK ,current val is %s\033[0m" %(zhulh,eval(service_data['data'])[zhulh])
                                W_to_list(new_zhulh)
                                '''
                                if new_zhulh not in warning_list or new_zhulh not in critical_list:
                                    if new_zhulh not in well_list:
                                        well_temp_list.append(new_zhulh)
                                        print well_temp_list
                                    #if zhulh not in well_list and cross_well_count >= 3:
                                    if new_zhulh not in well_list and well_temp_list.count(new_zhulh) >= 3:
                                        for i in range(well_temp_list.count(new_zhulh)):
                                            well_temp_list.remove(new_zhulh)
                                        well_list.append(new_zhulh)
                                        if new_zhulh in warning_list:
                                            warning_list.remove(new_zhulh)
                                        if new_zhulh in critical_list:                                    
                                            critical_list.remove(new_zhulh)
                                            '''
                            else:
                                C_to_list(new_zhulh)
                                '''
                                if new_zhulh in critical_list:
                                    for i in range(critical_temp_list.count(new_zhulh)):
                                            critical_temp_list.remove(new_zhulh)
                                else:
                                    critical_temp_list.append(new_zhulh)
                                    if critical_temp_list.count(new_zhulh) >= 3:                                        
                                        critical_list.append(new_zhulh)
                                        if new_zhulh in well_list:                                        
                                            well_list.remove(new_zhulh)
                                            '''

                        elif operators[operators_num] == 'eq':
                            if float(service[key]) == float(eval(service_data['data'])[zhulh]):
                                print "\033[32;1mService %s crossed OK ,current val is %s\033[0m" %(zhulh,eval(service_data['data'])[zhulh])
                                W_to_list(new_zhulh)
                                '''
                                if new_zhulh not in warning_list or new_zhulh not in critical_list:
                                    if new_zhulh not in well_list:
                                        well_temp_list.append(new_zhulh)
                                        print well_temp_list
                                    #if zhulh not in well_list and cross_well_count >= 3:
                                    if new_zhulh not in well_list and well_temp_list.count(new_zhulh) >= 3:
                                        for i in range(well_temp_list.count(new_zhulh)):
                                            well_temp_list.remove(new_zhulh)
                                        well_list.append(new_zhulh)
                                        if new_zhulh in warning_list:
                                            warning_list.remove(new_zhulh)
                                        if new_zhulh in critical_list:                                    
                                            critical_list.remove(new_zhulh)
                                            '''
                            else:
                                C_to_list(new_zhulh)
                                '''
                                if new_zhulh in critical_list:
                                    for i in range(critical_temp_list.count(new_zhulh)):
                                            critical_temp_list.remove(new_zhulh)
                                else:
                                    critical_temp_list.append(new_zhulh)
                                    if critical_temp_list.count(new_zhulh) >= 3:                                        
                                        critical_list.append(new_zhulh)
                                        if new_zhulh in well_list:                                        
                                            well_list.remove(new_zhulh)
                                            '''
                                            
                        new_service_data = {"service_name": service_data['service_name'], "hostname": service_data['hostname'], "data": "{'status': 1, '%s': '11'}" %zhulh, "time_stamp": service_data['time_stamp']}
                        print json.dumps(new_service_data)
                        redis.r[redis_key] = json.dumps(new_service_data)
                        print 'well_temp_list:',well_temp_list
                        print 'critical_temp_list:',critical_temp_list
                        new_service = service_name+'_'+zhulh
                        num = eval(service_data['data'])[zhulh]
                        cmd_sql = "insert into web_monitor_meg(hostname,service_name,num,time) values('"+host_name[host_id]+"','"+new_service+"','"+num+"','"+ service_data['time_stamp']+"')"
                        #W_mysql(cmd_sql,num=2)
                    else:
                        pass
            else:
                pass
    redis.r['critical::list'] = json.dumps(critical_list)
    redis.r['well::list'] = json.dumps(well_list)
    print 'The well_list is:','\033[37m%s\033[0m' %well_list
    print 'The critical_list is:','\033[31m%s\033[0m' %critical_list

def W_to_list(new_zhulh):
    if new_zhulh not in warning_list or new_zhulh not in critical_list:
        if new_zhulh not in well_list:
            well_temp_list.append(new_zhulh)
            print well_temp_list
        #if zhulh not in well_list and cross_well_count >= 3:
        if new_zhulh not in well_list and well_temp_list.count(new_zhulh) >= 3:
            for i in range(well_temp_list.count(new_zhulh)):
                well_temp_list.remove(new_zhulh)
            well_list.append(new_zhulh)
            if new_zhulh in warning_list:
                warning_list.remove(new_zhulh)
            if new_zhulh in critical_list:                                    
                critical_list.remove(new_zhulh)   


def C_to_list(new_zhulh):
    if new_zhulh in critical_list:
        for i in range(critical_temp_list.count(new_zhulh)):
            critical_temp_list.remove(new_zhulh)
    else:
        critical_temp_list.append(new_zhulh)
        if critical_temp_list.count(new_zhulh) >= 3:                                        
            critical_list.append(new_zhulh)
            if new_zhulh in well_list:                                        
                well_list.remove(new_zhulh)

def W_mysql(sql_cmd,num):
    try:
        conn = MySQLdb.connect(host='localhost',user='root',passwd='123456',db='s6monitor2',unix_socket='/tmp/mysql_3308.sock',port=3308)
        cur = conn.cursor()
        cur.execute(sql_cmd)
        if num == 1:  
            res = cur.fetchall()
           
            for line in res:
                num = str(line[0]).strip('L')
                host_name[num] = line[1]
        cur.close()
        conn.close()
        return host_name
    except MySQLdb.Error,e:
        print 'Mysql Error Msg:',e
    
    

if __name__ == '__main__':
    host_name = W_mysql('SELECT id,hostname FROM web_host',num=1)
    while True:
        fetch_monitored_list(host_name)
        time.sleep(5)