-
Notifications
You must be signed in to change notification settings - Fork 60
/
init_db.py
113 lines (104 loc) · 4.69 KB
/
init_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: toddler
# date: 20170430
import sqlite3
import inspect
class NSDb:
def __init__(self):
self.db_name = "ns.db"
self.conn = sqlite3.connect(self.db_name)
self.cur = self.conn.cursor()
def init_ns_base_tb(self):
init_sql = ('\n'
' CREATE TABLE IF NOT EXISTS ns_base (\n'
' ID INTEGER PRIMARY KEY AUTOINCREMENT,\n'
' HOSTNAME TEXT NOT NULL,\n'
' IP TEXT NOT NULL,\n'
' CAPTURETIME INTEGER NOT NULL,\n'
' CPU TEXT NOT NULL,\n'
' MEM TEXT NOT NULL,\n'
' SWAP TEXT NOT NULL,\n'
' OSNAME TEXT NOT NULL,\n'
' KERNEL TEXT NOT NULL,\n'
' UPTIME TEXT NOT NULL\n'
' )\n'
' ')
creat_base_index_sql = "CREATE UNIQUE INDEX [BASE_INDEX] ON [ns_base] ([HOSTNAME], [IP]);"
self.cur.execute(init_sql)
try:
self.cur.execute(creat_base_index_sql)
except Exception as err:
print err
self.conn.commit()
def init_ns_cpu_tb(self):
init_sql = ('\n'
' CREATE TABLE IF NOT EXISTS ns_cpu (\n'
' ID INTEGER PRIMARY KEY AUTOINCREMENT,\n'
' HOSTNAME TEXT NOT NULL,\n'
' IP TEXT NOT NULL,\n'
' CAPTURETIME INTEGER NOT NULL,\n'
' USER_CPU INTEGER NOT NULL,\n'
' NICE_CPU INTEGER NOT NULL,\n'
' SYSTEM_CPU INTEGER NOT NULL,\n'
' IOWAIT_CPU INTEGER NOT NULL,\n'
' IDLE_CPU INTEGER NOT NULL,\n'
' USAGE_CPU INTEGER NOT NULL,\n'
' LOAD_AVG INTEGER NOT NULL\n'
' )\n'
' ')
self.cur.execute(init_sql)
self.conn.commit()
def init_ns_mem_tb(self):
init_sql = ('\n'
' CREATE TABLE IF NOT EXISTS ns_mem (\n'
' ID INTEGER PRIMARY KEY AUTOINCREMENT,\n'
' HOSTNAME TEXT NOT NULL,\n'
' IP TEXT NOT NULL,\n'
' CAPTURETIME INTEGER NOT NULL,\n'
' MEM_TOTAL INTEGER NOT NULL,\n'
' MEM_USAGE INTEGER NOT NULL,\n'
' MEM_FREE INTEGER NOT NULL,\n'
' SWAP_TOTAL INTEGER NOT NULL,\n'
' SWAP_USED INTEGER NOT NULL,\n'
' SWAP_FREE INTEGER NOT NULL,\n'
' MEM_PERCENT INTEGER NOT NULL\n'
' )\n'
' ')
self.cur.execute(init_sql)
self.conn.commit()
def init_ns_disk_tb(self):
init_sql = ('\n'
' CREATE TABLE IF NOT EXISTS ns_disk (\n'
' ID INTEGER PRIMARY KEY AUTOINCREMENT,\n'
' HOSTNAME TEXT NOT NULL,\n'
' IP TEXT NOT NULL,\n'
' CAPTURETIME INTEGER NOT NULL,\n'
' DISK TEXT NOT NULL,\n'
' DISK_IO TEXT NOT NULL\n'
' )\n'
' ')
self.cur.execute(init_sql)
self.conn.commit()
def init_ns_net_tb(self):
init_sql = ('\n'
' CREATE TABLE IF NOT EXISTS ns_net (\n'
' ID INTEGER PRIMARY KEY AUTOINCREMENT,\n'
' HOSTNAME TEXT NOT NULL,\n'
' IP TEXT NOT NULL,\n'
' CAPTURETIME INTEGER NOT NULL,\n'
' INTERFACE TEXT NOT NULL,\n'
' TRAFFIC_IN INTEGER NOT NULL,\n'
' TRAFFIC_OUT INTEGER NOT NULL,\n'
' SOCKETS TEXT NOT NULL\n'
' )\n'
' ')
self.cur.execute(init_sql)
self.conn.commit()
def run_all_init_func(self):
"""
自动获取NSDb类里的所有init_ns_xxx方法,依次执行建表
"""
for func in inspect.getmembers(self, predicate=inspect.ismethod):
if func[0][:7] == 'init_ns':
func[1]()