Skip to content

Commit 775340d

Browse files
committed
added MAC search function
1 parent 725021d commit 775340d

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

cloudstackops/cloudstacksql.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ def getIpAddressData(self, ipaddress):
173173

174174
return result
175175

176+
# list mac adress info
177+
def getMacAddressData(self, macaddress):
178+
if not self.conn:
179+
return 1
180+
181+
cursor = self.conn.cursor()
182+
cursor.execute("SELECT networks.name, \
183+
nics.mac_address, \
184+
nics.ip4_address, \
185+
nics.netmask, \
186+
nics.broadcast_uri, \
187+
nics.mode, \
188+
nics.state, \
189+
nics.created, \
190+
vm_instance.name \
191+
FROM cloud.nics, cloud.vm_instance, \
192+
cloud.networks \
193+
WHERE nics.instance_id = vm_instance.id \
194+
AND nics.network_id = networks.id \
195+
AND mac_address \
196+
LIKE '%" + macaddress + "%' \
197+
AND nics.removed is null;")
198+
result = cursor.fetchall()
199+
cursor.close()
200+
201+
return result
202+
176203
# get uuid of router volume
177204
def getRouterRootVolumeUUID(self, routeruuid):
178205
if not self.conn:

whoHasThisMac.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/python
2+
3+
# Copyright 2015, Schuberg Philis BV
4+
#
5+
# Licensed to the Apache Software Foundation (ASF) under one
6+
# or more contributor license agreements. See the NOTICE file
7+
# distributed with this work for additional information
8+
# regarding copyright ownership. The ASF licenses this file
9+
# to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance
11+
# with the License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing,
16+
# software distributed under the License is distributed on an
17+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
# KIND, either express or implied. See the License for the
19+
# specific language governing permissions and limitations
20+
# under the License.
21+
22+
# Script to display who uses a given mac address (based on whoHasThisIp.py)
23+
# Edwin Beekman - ebeekman@schubergphilis.com
24+
25+
import time
26+
import sys
27+
import getopt
28+
from cloudstackops import cloudstacksql
29+
import os.path
30+
from random import choice
31+
from prettytable import PrettyTable
32+
33+
# Function to handle our arguments
34+
35+
36+
def handleArguments(argv):
37+
global DEBUG
38+
DEBUG = 0
39+
global DRYRUN
40+
DRYRUN = 0
41+
global mysqlHost
42+
mysqlHost = ''
43+
global mysqlPasswd
44+
mysqlPasswd = ''
45+
global macaddress
46+
macaddress = ''
47+
48+
# Usage message
49+
help = "Usage: ./" + os.path.basename(__file__) + ' [options] ' + \
50+
'\n --mac-address -m <mac address>\t\tSearch for this mac address ' + \
51+
'(partial is allowed)' + \
52+
'\n --mysqlserver -s <mysql hostname>\t\tSpecify MySQL server ' + \
53+
'to read HA worker table from' + \
54+
'\n --mysqlpassword <passwd>\t\t\tSpecify password to cloud ' + \
55+
'MySQL user' + \
56+
'\n --debug\t\t\t\t\tEnable debug mode' + \
57+
'\n --exec\t\t\t\t\tExecute for real (not needed for list* scripts)'
58+
59+
try:
60+
opts, args = getopt.getopt(
61+
argv, "hs:m:", [
62+
"mysqlserver=",
63+
"mac-address=",
64+
"mysqlpassword=",
65+
"debug",
66+
"exec"])
67+
except getopt.GetoptError as e:
68+
print "Error: " + str(e)
69+
print help
70+
sys.exit(2)
71+
for opt, arg in opts:
72+
if opt == '-h':
73+
print help
74+
sys.exit()
75+
elif opt in ("-s", "--mysqlserver"):
76+
mysqlHost = arg
77+
elif opt in ("-p", "--mysqlpassword"):
78+
mysqlPasswd = arg
79+
elif opt in ("-m", "--mac-address"):
80+
macaddress = arg
81+
elif opt in ("--debug"):
82+
DEBUG = 1
83+
elif opt in ("--exec"):
84+
DRYRUN = 0
85+
86+
# We need at least these vars
87+
if len(mysqlHost) == 0 or len(macaddress) == 0:
88+
print help
89+
sys.exit()
90+
91+
# Parse arguments
92+
if __name__ == "__main__":
93+
handleArguments(sys.argv[1:])
94+
95+
# Init our class
96+
s = cloudstacksql.CloudStackSQL(DEBUG, DRYRUN)
97+
98+
if DEBUG == 1:
99+
print "# Warning: Debug mode is enabled!"
100+
101+
if DRYRUN == 1:
102+
print "# Warning: dry-run mode is enabled, not running any commands!"
103+
104+
# Connect MySQL
105+
result = s.connectMySQL(mysqlHost, mysqlPasswd)
106+
if result > 0:
107+
print "Error: MySQL connection failed"
108+
sys.exit(1)
109+
elif DEBUG == 1:
110+
print "DEBUG: MySQL connection successful"
111+
print s.conn
112+
113+
macaddresses = s.getMacAddressData(macaddress)
114+
counter = 0
115+
t = PrettyTable(["VM name",
116+
"Network Name",
117+
"Mac Address",
118+
"Ipv4",
119+
"Netmask",
120+
"Mode",
121+
"State",
122+
"Created"])
123+
t.align["VM name"] = "l"
124+
t.align["Network Name"] = "l"
125+
126+
for (
127+
networkname,
128+
mac_address,
129+
ip4_address,
130+
netmask,
131+
broadcast_uri,
132+
mode,
133+
state,
134+
created,
135+
vmname) in macaddresses:
136+
counter = counter + 1
137+
138+
vmname = (vmname[:22] + '..') if len(vmname) > 24 else vmname
139+
networkname = (
140+
networkname[:22] + '..') if networkname is not None \
141+
and len(networkname) > 24 else networkname
142+
t.add_row([vmname,
143+
networkname,
144+
mac_address,
145+
ip4_address,
146+
netmask,
147+
mode,
148+
state,
149+
created])
150+
151+
# Disconnect MySQL
152+
s.disconnectMySQL()
153+
154+
print t
155+
print "Note: Found " + str(counter) + " results."

0 commit comments

Comments
 (0)