-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_view_instances.py
executable file
·163 lines (151 loc) · 5.52 KB
/
aws_view_instances.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
# aws_view_instances
#
# Author: E. van Nuil / Oblivion b.v.
# Project: Informa / Euroforum AWS
#
# Version 0.4
#
# 15-11-2012: 0.4 Adding Volume totals
# 12-09-2012: 0.3 Added Datatable (http://datatables.net)
# 11-09-2012: 0.2 Getting data and formatting
# 11-09-2012: 0.1 Initial setup, templating system
#
# Uses Mako for the HTML templating and Boto for the AWS functions
# sudo easy_install Mako
# sudo easy_install boto
#
# The file gets the region and the credentials from the .boto file
# ~/.boto:
# [Boto]
# ec2_region_name = eu-west-1
# ec2_region_endpoint = eu-west-1.ec2.amazonaws.com
#
# [Credentials]
# aws_access_key_id = XXXXXXX
# aws_secret_access_key = YYYYYYYYYYYYYYY
#
from mako.template import Template
from datetime import date
import datetime
import os
import boto
import boto.ec2
# TODO: Use proxy functions
proxyHost = ''
proxyPort = ''
if proxyHost == '':
ec2 = boto.connect_ec2()
else:
ec2 = boto.connect_ec2(proxy=proxyHost, proxy_port=proxyPort)
reservations = ec2.get_all_instances()
# Get all instances
instance = reservations[0].instances[0]
# Get the HTML template
mytemplate = Template(filename='report/report.html')
# Get the date
now = datetime.datetime.now()
# Create the text buffer which is passed to the Mako render function
buf = ""
# Use the current date
today = date.today()
# Loop through all the reservations and instances
for r in reservations:
for i in r.instances:
buf += "<tr>"
try:
buf += "<td><b>%s</b> (%s)</td><td><span class='label label-" % (i.tags['Name'],i.id)
except:
buf += "<td><b>No Name!</b> (%s)</td><td><span class='label label-" % i.id
if i.state == "running":
buf += "warning"
else:
buf += "info"
buf += "'>%s</span></td>\n" % i.state
# Start Tags
buf += "<td><div class=\"btn-group\">"
buf += "<a class=\"btn btn-mini dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">"
buf += "<i class=\"icon-tags\"></i> "
buf += "<span class=\"caret\"></span></a>"
buf += "<ul class=\"dropdown-menu\">"
for key,value in i.tags.items():
buf += "<li><a href=\"#\">%s: %s</a></li>" % (key,value)
buf += "</ul></div></td>"
# Start Addresses
buf += "<td><div class=\"btn-group\">"
buf += "<a class=\"btn btn-mini dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">"
buf += "Info "
buf += "<span class=\"caret\"></span></a>"
buf += "<ul class=\"dropdown-menu\">"
buf += "<li><a href=\"#\">Private ip: <b>%s</b></a></li>" % i.private_ip_address
buf += "<li><a href=\"#\">Public ip: <b>%s</b></a></li>" % i.ip_address
buf += "<li><a href=\"#\">Private DNS: <b>%s</b></a></li>" % i.private_dns_name
buf += "<li><a href=\"#\">Public DNS: <b>%s</b></a></li>" % i.public_dns_name
buf += "</ul></div></td>"
# Start VPC
buf += "<td>%s</td>" % i.vpc_id
# Start Type
try:
if i.instance_type[0:2] == "m1":
buf += "<td class=\"text-info\">%s</td>\n" % i.instance_type
elif i.instance_type[0:2] == "c1":
buf += "<td class=\"text-error\">%s</td>\n" % i.instance_type
elif i.instance_type[0:2] == "t1":
buf += "<td class=\"muted\">%s</td>\n" % i.instance_type
else:
buf += "<td class=\"text-warning\">%s</td>\n" % i.instance_type
except:
buf += "<td class=\"text-warning\">NaI</td>\n"
buf += "<td>"
if i.architecture == "i386":
buf += "<span class=\"badge\">32"
else:
buf += "<span class=\"badge badge-info\">64"
buf += "</span></td>"
buf += "<td>"
if i.monitored == True:
buf += "<i class=\"icon-check\"></i>"
buf += "</td>"
# Start Volumes
buf += "<td><div class=\"btn-group\">"
buf += "<a class=\"btn btn-mini dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">"
buf += "<i class=\"icon-hdd\"></i> "
buf += "<span class=\"caret\"></span></a>"
buf += "<ul class=\"dropdown-menu\">"
totalvolumesize = 0
for v in ec2.get_all_volumes(filters={'attachment.instance-id': i.id}):
buf += "<li><a href=\"#" + v.id + "\">"
buf += v.id + " (" + unicode(v.size) + "GB)"
totalvolumesize += v.size
buf += "</a></li>"
buf += "</ul></div></td>"
buf += "<td>" + unicode(totalvolumesize) + "</td>"
buf += "</tr>\n"
buf += "</tbody>\n"
buf += "</table>"
totalvolumesizes = 0
for v in ec2.get_all_volumes():
totalvolumesizes += v.size
buf += "<br/><hr>Total volume sizes: <b>" + unicode(totalvolumesizes) + " GB</b>"
# TODO: write to file with date in the file name to a S3 bucket.
output = mytemplate.render(date=today,data=buf)
# Write mode creates a new file or overwrites the existing content of the file.
# Write mode will _always_ destroy the existing contents of a file.
try:
# This will create a new file or **overwrite an existing file**.
f = open(("report/%s_report.html" % (today)), "w")
try:
f.writelines(output) # Write a sequence of strings to a file
finally:
f.close()
except IOError:
pass
try:
# This will create a new file or **overwrite an existing file**.
f = open("report/index.html", "w")
try:
f.writelines(output) # Write a sequence of strings to a file
finally:
f.close()
except IOError:
pass