-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver_stats.py
49 lines (44 loc) · 1.46 KB
/
server_stats.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
import psutil
import os
import socket
from kombu import Connection, Exchange, Queue, Producer
import time
import sys
class ServerStat:
@staticmethod
def get_utilization():
result = {}
result['CPU'] = cpu_utilitzation = psutil.cpu_percent()
stats = dict(psutil.virtual_memory()._asdict())
result['RAM_USAGE'] = stats['used'] * 100.0 / stats['total']
return result
def get_server_stats():
result = ServerStat.get_utilization()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 1)) # connect() for UDP doesn't send packets
local_ip_address = s.getsockname()[0]
result['LocalIP'] = local_ip_address
'''
with open(config_file_name) as f:
for line in f.readlines():
key, value = line.split('=')
result[key] = value
'''
return result
argument_list = sys.argv
url = argument_list[1]
exchange_name = 'exchange-server-stats'
queue_name = 'server-stats-queue'
#config_file_name = argument_list[4]
rabbit_url = "amqp://admin:admin@" + url + ":5672/"
conn = Connection(rabbit_url)
channel = conn.channel()
exchange = Exchange(exchange_name, type="direct")
producer = Producer(exchange=exchange, channel=channel, routing_key='Stats123')
queue = Queue(name=queue_name, exchange=exchange, routing_key='Stats123')
queue.maybe_bind(conn)
while True:
msg = get_server_stats()
print(msg)
producer.publish(msg)
time.sleep(60)