forked from jordant/openstack-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nova-vm-count-by-status.py
executable file
·69 lines (52 loc) · 1.46 KB
/
nova-vm-count-by-status.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
#!/usr/bin/env python
import re
import os
import time
import sys
import socket
import novaclient.v1_1
STATS_PREFIX = "openstack.nova"
if not re.match(".$", STATS_PREFIX):
STATS_PREFIX += "."
USER=os.environ['OS_USERNAME']
PASS=os.environ['OS_PASSWORD']
TENANT_NAME=os.environ['OS_TENANT_NAME']
OS_AUTH_URL=os.environ['OS_AUTH_URL']
GRAPHITE_HOST="10.64.140.42"
def novaConnect():
return novaclient.v1_1.Client(USER, PASS, TENANT_NAME, OS_AUTH_URL, timeout="600", service_type="compute", no_cache=True)
def collect_metric(name, value, timestamp):
sock = socket.socket()
sock.settimeout(5)
sock.connect( (GRAPHITE_HOST, 2003) )
print "%s %s %d" % (STATS_PREFIX + name, value, timestamp)
sock.send("%s %s %d\n" % (STATS_PREFIX + name, value, timestamp))
sock.close()
def now():
return int(time.time())
def total_time(timings):
total = 0
for url, start, end in timings:
total += round(end - start,2)
return total
#main
nova = novaConnect()
status_count = {}
# servers timings/counts
try:
servers = nova.servers.list(True, {'all_tenants': '1'})
for server in servers:
status = server.status
if status not in status_count:
status_count[status] = 1
else:
status_count[status] += 1
except Exception as e:
print "servers.list failed %s" % e
for status in status_count:
print "%s %d", (status, status_count[status])
collect_metric("counts.servers.status." + status, status_count[status], now())
# exit cleanly
sys.stdout.flush()
sys.stderr.flush()
sys.exit(0)