1. 程式人生 > >sqlite處理資料

sqlite處理資料

# coding: UTF-8
import platform
from _utils.patrol2 import run_cmd, data_format, report_format
import os, sys, re, json, copy
import stat
import socket
import stat
import sqlite3

#
# conn=sqlite3.connect(':memory:')
if os.path.exists('was_patrol.db'):
    os.remove('was_patrol.db')
conn = sqlite3.connect('was_patrol.db')
c = conn.cursor()
c.execute('''CREATE TABLE WAS
       (ID INT PRIMARY KEY     NOT NULL,
       CELL           CHAR(50),
       HOME         CHAR(50),
       CLUSTER        CHAR(50),
       NODE           CHAR(50),
       SERVER        CHAR(50),
       STATE    CHAR(50),
       PORTS    CHAR(500),
       HOST    CHAR(50));''')
print "Table created successfully"
conn.commit()

version = version.split('.')[0]
content = """import sys, java
def get_names(type,parent_obj=None):
    if not parent_obj:
        obj_list = AdminConfig.list(type)
    else:
        obj_list = AdminConfig.list(type, parent_obj)
    if len(obj_list) == 0:
        return [],None
    obj_lists = obj_list.split(lineSeparator)
    names=[]
    for obj in obj_lists:
        name = AdminConfig.showAttribute(obj, "name")
        names.append(name)
    return names,obj_lists

lineSeparator = java.lang.System.getProperty('line.separator')

cell_names,cells=get_names("Cell")
reports = []
print cell_names,cells
for cell in cells:
    cell_name=AdminConfig.showAttribute(cell, "name")

    cluster_names,clusters=get_names("ServerCluster",parent_obj=cell)
    node_names, nodes = get_names("Node", parent_obj=cell)

    for node in nodes:
        nname = AdminConfig.showAttribute(node, "name")
        server_names, servers = get_names("Server", parent_obj=node)
        hostname= AdminConfig.showAttribute(node, "hostName")

        print 'node:',node
        print 'servers:',servers
        for server in servers:
            type=AdminConfig.showAttribute(server, "serverType")
            if type=='NODE_AGENT':
                continue
            sname = AdminConfig.showAttribute(server, "name")
            cname = AdminConfig.showAttribute(server, "clusterName")

            runserv = AdminConfig.getObjectName(server)
            if len(runserv) > 0:
                state = AdminControl.getAttribute(runserv, "state")
            else:
                state='down'

            other_port=id=''
            if int('{version}')>=6:
                ports=AdminTask.listServerPorts(sname, '[-nodeName '+nname+']')
            else:
                ports=[]
                endpoints=AdminConfig.list('EndPoint', server).split(lineSeparator)
                print endpoints
                #endpoints=AdminConfig.list('EndPoint', AdminConfig.getid( '/Cell:'+cell_name+'/Node:'+nname+'/Server:'+sname+'/')).split(lineSeparator)
                for endpoint in endpoints[1:]:
                    end_result=AdminConfig.show(endpoint).split(lineSeparator)
                    print end_result
                    if end_result:
                        ports.append(end_result[1].split()[1].split(']')[0])
                ports=','.join(ports)
                print ports


                id=AdminConfig.getid( '/Cell:'+cell_name+'/VirtualHost:default_host/')
                if not id:
                    id=AdminConfig.getid( '/Cell:'+cell_name+'/VirtualHost:admin_host/')
                if id:
                    other_port=AdminConfig.list('HostAlias',id ).split(lineSeparator)[0]
                    other_port=AdminConfig.showAttribute(other_port,'port')
            if not cname:
                # no cluster and has server
                reports.append({{'cell': cell_name, 'cluster': 'N/A', 'server': sname, 'node': nname,'obj':server,'state':state,'ports':ports,'host':hostname,'other_port':other_port}})
            else:
                # has cluster and node
                reports.append({{'cell': cell_name, 'cluster': cname, 'server': sname, 'node': nname,
                                'obj': server,'state':state,'ports':ports,'host':hostname,'other_port':other_port}})
    print 'clusters',clusters
    reports1=[]
    if clusters:
        for cluster in clusters:
            cluster_name = AdminConfig.showAttribute(cluster, "name")
            memberList = AdminConfig.showAttribute(cluster, "members")

            if memberList!='[]' and memberList!=[] :
                #has cluster and node
                #pass
                members = memberList[1:len(memberList)-1].split(" ")
                for member in members:
                    mname = AdminConfig.showAttribute(member, "memberName")
                    nname = AdminConfig.showAttribute(member, "nodeName")
                    reports1.append({{'cell':cell_name,'cluster':cluster_name,'server':mname,'node':nname,'obj':server}})
            else:
                # has cluster and no node
                reports1.append({{'cell':cell_name,'cluster':cluster_name,'server':'N/A','node':'N/A','obj':None}})

print "==data=="
print reports
print "==data=="
print "==datas=="
print reports1
print "==datas=="
""".format(version=version)


if 'Windows' in platform.system():
    tmp_script = 'C:\\patrol.py'
else:
    tmp_script = '/tmp/patrol.py'
with open(tmp_script, 'w') as f:
    f.write(content)
os.chmod(tmp_script, stat.S_IRWXU | stat.S_IRGRP | stat.S_IROTH)

hostname = platform.node()

was_homes = was_home
datas = {'cell': [], 'cluster': [], 'node': [], 'server': []}
all_reports = []
cell_reports = []
cluster_reports = []
server_reports = []
resource = []
count = 0
for was_home in was_homes.split(','):
    run_script = os.path.join('{}'.format(was_home), 'bin/wsadmin.sh')
    if mw_password:
        password = '-password {}'.format(mw_password)
    else:
        password = ''

    if 'Windows' in platform.system():
        run_script = os.path.join('{}'.format(was_home), 'bin', 'wsadmin.bat')
        cmd = '"{}" -username {} {} -lang jython -f {}'.format(run_script, mw_user, password,
                                                               tmp_script)
    else:
        cmd = 'su {} -c "{} -username {} {} -lang jython -f {}"'.format(os_was_user,
                                                                        run_script, mw_user,
                                                                        password,
                                                                        tmp_script)
    print cmd
    code, out = run_cmd(cmd)
    print out
    if 'Ensure that user and password are correct' in out:
        print '請確認輸入的使用者名稱和密碼是否正確'
        sys.exit(1)
    if code:
        print out
        sys.exit(1)
    try:
        res = re.findall(r'==data==([\s\S]*)==data==', out)
        if not res:
            print '無輸出'
            sys.exit(1)
        res = eval(res[0].strip())

        res1 = re.findall(r'==datas==([\s\S]*)==datas==', out)
        if not res1:
            print '無輸出'
            sys.exit(1)
        res1 = eval(res1[0].strip())
        print '叢集資訊', res1

    except Exception as e:
        print e
        sys.exit(1)
    resource.append({'res': res, 'washome': was_home})

    cell_name = None
    for i in res:
        cell_name = i['cell']
        if i['cell'] not in datas['cell']:
            datas['cell'].append(i['cell'])
        if i['cluster'] not in datas['cluster']:
            datas['cluster'].append(i['cluster'])
        if i['node'] not in datas['node']:
            datas['node'].append(i['node'])
        if i['server'] not in datas['server']:
            datas['server'].append(i['server'])

        c = conn.cursor()

        port = ''
        ports = i.get('ports', '')
        if int(version) >= 6:
            for j in ports.split('\n'):
                if j.startswith('[[WC_adminhost ') and i['server'] == 'dmgr':
                    port = j.rstrip('] ]]] ]').split(' ')[-1]
            if not port:
                for j in ports.split('\n'):
                    if j.startswith('[[WC_defaulthost '):
                        port = j.rstrip('] ]]] ]').split(' ')[-1]
            if not port:
                for j in ports.split('\n'):
                    if j.startswith('[[WEBSERVER_ADDRESS '):
                        port = j.rstrip('] ]]] ]').split(' ')[-1]
        else:
            print '端點:', ports
            import requests

            for j in ports.split(','):
                print j
                try:
                    res = requests.get('http://localhost:{}/snoop'.format(j))
                    if res.status_code == 200:
                        port = j
                        break
                    if not port:
                        res = requests.get('https://localhost:{}/snoop'.format(j), is_verify=False)
                        if res.status_code == 200:
                            port = j
                            break
                except:
                    pass
            if not port:
                port = ports.split(',')[0]

        print 'port',port

        state= i.get('state','')
        if int(version) <= 5:
            cmd = "netstat -Aan|grep -w '*.{}'".format(port)
            code, res = run_cmd(cmd)
            if res and 'LISTEN' in res.split()[-1]:
                state = 'STARTED'

        c.execute("INSERT INTO WAS (ID,CELL,HOME,CLUSTER,NODE,SERVER,STATE,PORTS,HOST) \
                     VALUES ({}, '{}', '{}', '{}', '{}','{}','{}','{}','{}' )".format(count,
                                                                                      i['cell'],
                                                                                      was_home,
                                                                                      i['cluster'],
                                                                                      i['node'],
                                                                                      i['server'],state,
                                                                                      port,
                                                                                      i.get('host',
                                                                                            '')))
        count += 1

        conn.commit()
    print datas

cursor = conn.execute("SELECT distinct CELL from WAS")
cells = []
for row in cursor:
    cell = row[0]
    if cell not in cells:
        cells.append(cell)
    cell_reports.append(data_format('單元名稱', cell, alert=0, group_name="單元情況"))
cell_num = len(cells)
if cell_num == 1:
    all_reports.append(report_format("{}".format(hostname), cell_reports))
else:
    all_reports.append(report_format("{}".format(hostname), childs=cell_reports))

cell_clusters = []
cell_cluster_report = {}
cursor = conn.execute("SELECT id, cell, home,cluster,node, server,state,ports,host  from WAS")
for row in cursor:
    cell = row[1]
    node = row[4]
    server = row[5]
    home = row[2]
    cluster = row[3]
    if cell_num == 1:
        cell_cluster = '{}'.format(hostname)
    else:
        cell_cluster = '{}/{}'.format(hostname, cell)
    if cell_cluster not in cell_clusters:
        cell_cluster_report.update({cell_cluster: []})
        cell_clusters.append(cell_cluster)
    if cluster not in cell_cluster_report[cell_cluster]:
        cell_cluster_report[cell_cluster].append(cluster)

reports = []
for k, v in cell_cluster_report.items():
    for i in v:
        reports.append(data_format('叢集名稱', i, alert=0, group_name="叢集名稱"))
    all_reports.append(report_format(k, childs=reports))

reports = json.dumps(all_reports)
print reports