Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e76900e

Browse files
committed
服务器信息采集上传
1 parent 28c99f6 commit e76900e

25 files changed

Lines changed: 347 additions & 0 deletions

agent/UUID

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9e468e98fa6711e799d2784f439c1853
427 Bytes
Binary file not shown.

agent/agentd.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#encoding: utf-8
2+
3+
import os
4+
import argparse
5+
import logging
6+
import time
7+
8+
import gconf
9+
from utils import fileutils, sysutils
10+
from utils import mqueue
11+
12+
from plugins.ens import ENS
13+
from plugins.client import Client
14+
from plugins.heartbeat import Heartbeat
15+
from plugins.resource import Resource
16+
17+
logger = logging.getLogger(__name__)
18+
19+
parser = argparse.ArgumentParser(prog='agentd', add_help=False)
20+
parser.add_argument('-H', '--host', dest='host', help='', nargs='+')
21+
parser.add_argument('-P', '--port', dest='port', help='', nargs='+')
22+
parser.add_argument('-V', '--verbose', dest='detail', help='', action='store_true')
23+
args = parser.parse_args()
24+
25+
def main(config):
26+
ths = {}
27+
28+
ths['ens'] = {'class' : ENS, 'threading': None}
29+
ths['client'] = {'class' : Client, 'threading': None}
30+
ths['heartbeat'] = {'class' : Heartbeat, 'threading': None}
31+
ths['resource'] = {'class' : Resource, 'threading': None}
32+
33+
while True:
34+
for key, value in ths.items():
35+
if value['threading'] is None or not value['threading'].is_alive():
36+
logger.error('threading[%s] is dead and restart', key)
37+
value['threading'] = value['class'](config)
38+
value['threading'].start()
39+
40+
time.sleep(3)
41+
42+
43+
44+
if __name__ == '__main__':
45+
46+
config = gconf.Config
47+
48+
HOST = args.host[0]
49+
PORT = args.port[0]
50+
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
51+
PID = os.getpid()
52+
53+
if args.detail:
54+
logging.basicConfig(
55+
level=logging.DEBUG,
56+
format='%(asctime)s - %(name)s - %(levelname)s:%(message)s',
57+
filename=os.path.join(PROJECT_PATH, 'logs', 'agentd.log'),
58+
filemode='w',
59+
)
60+
else:
61+
logging.basicConfig(
62+
level=logging.INFO,
63+
format='%(asctime)s - %(name)s - %(levelname)s:%(message)s',
64+
filename=os.path.join(PROJECT_PATH, 'logs', 'agentd.log'),
65+
filemode='w',
66+
)
67+
68+
setattr(config, 'HOST', HOST)
69+
setattr(config, 'PORT', PORT)
70+
setattr(config, 'PROJECT_PATH', PROJECT_PATH)
71+
setattr(config, 'PID', PID)
72+
73+
setattr(config, 'QUEUE', mqueue.Queue())
74+
75+
PATH_UUID = os.path.join(PROJECT_PATH, 'UUID')
76+
UUID = fileutils.read_file(PATH_UUID)
77+
if not UUID:
78+
UUID = sysutils.get_uuid()
79+
fileutils.write_file(PATH_UUID, UUID)
80+
81+
setattr(config, 'UUID', UUID)
82+
83+
logger.info('agentd is starting...')
84+
logger.info('PID: %s', PID)
85+
logger.info('UUID: %s', UUID)
86+
87+
main(config)

agent/gconf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#encoding: utf-8
2+
class Config(object):
3+
pass
4+
5+
# 获取代码运行位置 PROJECT_PATH
6+
if __name__ == '__main__':
7+
import pathlib
8+
setattr(Config, 'PROJECT_PATH', pathlib.Path(__file__).absolute())
9+
print(getattr(Config, 'PROJECT_PATH'))

agent/logs/agentd.log

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2018-01-18 09:36:42,031 - __main__ - INFO:agentd is starting...
2+
2018-01-18 09:36:42,032 - __main__ - INFO:PID: 1599
3+
2018-01-18 09:36:42,032 - __main__ - INFO:UUID: 9e468e98fa6711e799d2784f439c1853
4+
2018-01-18 09:36:42,032 - __main__ - ERROR:threading[ens] is dead and restart
5+
2018-01-18 09:36:42,038 - __main__ - ERROR:threading[client] is dead and restart
6+
2018-01-18 09:36:42,049 - __main__ - ERROR:threading[heartbeat] is dead and restart
7+
2018-01-18 09:36:42,058 - __main__ - ERROR:threading[resource] is dead and restart

agent/plugins/__init__.py

Whitespace-only changes.
130 Bytes
Binary file not shown.
1.04 KB
Binary file not shown.
1.13 KB
Binary file not shown.
1.31 KB
Binary file not shown.

0 commit comments

Comments
 (0)