forked from rvojcik/rtapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
716 lines (572 loc) · 30.4 KB
/
__init__.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#!/usr/bin/python
#
# RTAPI
# Racktables API is simple python module providing some methods
# for monipulation with racktables objects.
#
# This utility is released under GPL v2
#
# Server Audit utility for Racktables Datacenter management project.
# Copyright (C) 2012 Robert Vojcik ([email protected])
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
'''PyRacktables
Simple python Class for manipulation with objects in racktables database.
For proper function, some methods need ipaddr module (https://pypi.python.org/pypi/ipaddr)
'''
__author__ = "Robert Vojcik ([email protected])"
__version__ = "0.1.3"
__copyright__ = "OpenSource"
__license__ = "GPLv2"
__all__ = ["RTObject"]
import re
import ipaddr
class RTObject:
'''Ractables object. Require database object as argument. '''
# Init method
def __init__(self, dbobject):
'''Initialize Object'''
# Open configuration file
self.db = dbobject
self.dbresult = self.db.cursor()
# DATABASE methods
def db_query_one(self, sql):
'''SQL query function, return one row. Require sql query as parameter'''
self.dbresult.execute(sql)
return self.dbresult.fetchone()
def db_query_all(self, sql):
'''SQL query function, return all rows. Require sql query as parameter'''
self.dbresult.execute(sql)
return self.dbresult.fetchall()
def db_insert(self, sql):
'''SQL insert/update function. Require sql query as parameter'''
self.dbresult.execute(sql)
self.db.commit()
def db_fetch_lastid(self):
'''SQL function which return ID of last inserted row.'''
return self.dbresult.lastrowid
def ListObjects(self):
'''List all objects'''
sql = 'SELECT name FROM Object'
return "Found " + str(len(self.db_query_all(sql))) +" objects in database"
# Object methotds
def ObjectExistST(self,service_tag):
'''Check if object exist in database based on asset_no'''
sql = 'SELECT name FROM Object WHERE asset_no = \''+service_tag+'\''
if self.db_query_one(sql) == None:
return False
else:
return True
def ObjectExistName(self,name):
'''Check if object exist in database based on name'''
sql = 'select id from Object where name = \''+name+'\''
if self.db_query_one(sql) == None:
return False
else:
return True
def ObjectExistSTName(self,name,asset_no):
'''Check if object exist in database based on name'''
sql = "SELECT id FROM Object WHERE name = '%s' AND asset_no = '%s'" % (name,asset_no)
if self.db_query_one(sql) == None:
return False
else:
return True
def AddObject(self,name,server_type_id,asset_no,label):
'''Add new object to racktables'''
sql = "INSERT INTO Object (name,objtype_id,asset_no,label) VALUES ('%s',%d,'%s','%s')" % (name,server_type_id,asset_no,label)
self.db_insert(sql)
def UpdateObjectLabel(self,object_id,label):
'''Update label on object'''
sql = "UPDATE Object SET label = '%s' where id = %d" % (label, object_id)
self.db_insert(sql)
def UpdateObjectComment(self,object_id,comment):
'''Update comment on object'''
sql = "UPDATE Object SET comment = '%s' where id = %d" % (comment, object_id)
self.db_insert(sql)
def UpdateObjectName(self,object_id,name):
'''Update name on object'''
sql = "UPDATE Object SET name = '%s' where id = %d" % (name, object_id)
self.db_insert(sql)
def GetObjectName(self,object_id):
'''Translate Object ID to Object Name'''
#Get interface id
sql = "SELECT name FROM Object WHERE id = %d" % (object_id)
result = self.db_query_one(sql)
if result != None:
object_name = result[0]
else:
object_name = None
return object_name
def GetObjectNameByAsset(self,service_tag):
'''Translate Object AssetTag to Object Name'''
#Get interface id
sql = "SELECT name FROM Object WHERE asset_no = '%s'" % (service_tag)
result = self.db_query_one(sql)
if result != None:
object_name = result[0]
else:
object_name = None
return object_name
def GetObjectLabel(self,object_id):
'''Get object label'''
#Get interface id
sql = "SELECT label FROM Object WHERE id = %d" % (object_id)
result = self.db_query_one(sql)
if result != None:
object_label = result[0]
else:
object_label = None
return object_label
def GetObjectComment(self,object_id):
'''Get object comment'''
#Get interface id
sql = "SELECT comment FROM Object WHERE id = %d" % (object_id)
result = self.db_query_one(sql)
if result != None:
object_comment = result[0]
else:
object_comment = None
return object_comment
def GetObjectTags(self,object_id):
'''Get object tags'''
sql = "SELECT t1.tag as parent_tag, t2.tag as tag FROM TagTree as t1 RIGHT JOIN TagTree as t2 ON t1.id = t2.parent_id WHERE t2.id IN (SELECT tag_id FROM TagStorage JOIN Object ON TagStorage.entity_id = Object.id WHERE TagStorage.entity_realm='object' and Object.id = '%d')" % (object_id)
result = self.db_query_all(sql)
return result
def GetObjectId(self,name):
'''Translate Object name to object id'''
#Get interface id
sql = "SELECT id FROM Object WHERE name = '%s'" % (name)
result = self.db_query_one(sql)
if result != None:
object_id = result[0]
else:
object_id = None
return object_id
# Logging
def InsertLog(self,object_id,message):
'''Attach log message to specific object'''
sql = "INSERT INTO ObjectLog (object_id,user,date,content) VALUES (%d,'script',now(),'%s')" % (object_id, message)
self.db_insert(sql)
# Attrubute methods
def InsertAttribute(self,object_id,object_tid,attr_id,string_value,uint_value,name):
'''Add or Update object attribute.
Require 6 arguments: object_id, object_tid, attr_id, string_value, uint_value, name'''
# Check if attribute exist
sql = "SELECT string_value,uint_value FROM AttributeValue WHERE object_id = %d AND object_tid = %d AND attr_id = %d" % (object_id, object_tid, attr_id)
result = self.db_query_one(sql)
if result != None:
# Check if attribute value is same and determine attribute type
old_string_value = result[0]
old_uint_value = result[1]
same_flag = "no"
attribute_type = "None"
if old_string_value != None:
attribute_type = "string"
old_value = old_string_value
if old_string_value == string_value:
same_flag = "yes"
elif old_uint_value != None:
attribute_type = "uint"
old_value = old_uint_value
if old_uint_value == uint_value:
same_flag = "yes"
# If exist, update value
new_value = ''
if same_flag == "no":
if attribute_type == "string":
sql = "UPDATE AttributeValue SET string_value = '%s' WHERE object_id = %d AND attr_id = %d AND object_tid = %d" % (string_value, object_id, attr_id, object_tid)
new_value = string_value
if attribute_type == "uint":
sql = "UPDATE AttributeValue SET uint_value = %d WHERE object_id = %d AND attr_id = %d AND object_tid = %d" % (uint_value, object_id, attr_id, object_tid)
new_value = uint_value
self.db_insert(sql)
else:
# Attribute not exist, insert new
if string_value == "NULL":
sql = "INSERT INTO AttributeValue (object_id,object_tid,attr_id,uint_value) VALUES (%d,%d,%d,%d)" % (object_id,object_tid,attr_id,uint_value)
else:
sql = "INSERT INTO AttributeValue (object_id,object_tid,attr_id,string_value) VALUES (%d,%d,%d,'%s')" % (object_id,object_tid,attr_id,string_value)
self.db_insert(sql)
def GetAttributeId(self,searchstring):
'''Search racktables database and get attribud id based on search string as argument'''
sql = "SELECT id FROM Attribute WHERE name LIKE '%"+searchstring+"%'"
result = self.db_query_one(sql)
if result != None:
getted_id = result[0]
else:
getted_id = None
return getted_id
def GetAttributeValue(self,object_id,attr_id):
'''Search racktables database and get attribute values'''
sql = "SELECT string_value,uint_value,float_value FROM AttributeValue WHERE object_id = %d AND attr_id = %d" % (object_id, attr_id)
result = self.db_query_one(sql)
if result != None:
output = [result[0], result[1], result[2]]
else:
output = None
return output
# Interfaces methods
def GetInterfaceName(self,object_id,interface_id):
'''Find name of specified interface. Required object_id and interface_id argument'''
#Get interface id
sql = "SELECT name FROM Port WHERE object_id = %d AND name = %d" % (object_id, interface_id)
result = self.db_query_one(sql)
if result != None:
port_name = result[0]
else:
port_name = None
return port_name
def GetInterfaceId(self,object_id,interface):
'''Find id of specified interface'''
#Get interface id
sql = "SELECT id,name FROM Port WHERE object_id = %d AND name = '%s'" % (object_id, interface)
result = self.db_query_one(sql)
if result != None:
port_id = result[0]
else:
port_id = None
return port_id
def UpdateNetworkInterface(self,object_id,interface):
'''Add network interfece to object if not exist'''
sql = "SELECT id,name FROM Port WHERE object_id = %d AND name = '%s'" % (object_id, interface)
result = self.db_query_one(sql)
if result == None:
sql = "INSERT INTO Port (object_id,name,iif_id,type) VALUES (%d,'%s',1,24)" % (object_id,interface)
self.db_insert(sql)
port_id = self.db_fetch_lastid()
else:
port_id = result[0]
return port_id
def GetPortDeviceNameById(self,port_id):
'''Get Device name and Port Name by port ID, return dictionary device_name, port_name'''
sql = "SELECT Port.name as port_name, Object.name as obj_name FROM Port INNER JOIN Object ON Port.object_id = Object.id WHERE Port.id = %d;" % (port_id)
result = self.db_query_one(sql);
if result == None:
return result
else:
port_name = result[0]
device_name = result[1]
return {'device_name': device_name, 'port_name': port_name}
def LinkNetworkInterface(self,object_id,interface,switch_name,interface_switch):
'''Link two devices togetger'''
#Get interface id
port_id = self.GetInterfaceId(object_id,interface)
if port_id != None:
#Get switch object ID
switch_object_id = self.GetObjectId(switch_name)
if switch_object_id != None:
switch_port_id = self.GetInterfaceId(switch_object_id,interface_switch)
if switch_port_id != None:
if switch_port_id > port_id:
select_object = 'portb'
else:
select_object = 'porta'
# Check server interface, update or create new link
sql = "SELECT %s FROM Link WHERE porta = %d OR portb = %d" % (select_object, port_id, port_id)
result = self.db_query_one(sql)
if result == None:
# Check if switch port is connected to another server
sql = "SELECT porta,portb FROM Link WHERE porta = %d OR portb = %d" % (switch_port_id, switch_port_id)
result = self.db_query_one(sql)
if result != None:
# Get ports id of old link
old_link_a, old_link_b = result
old_link_a_dict = self.GetPortDeviceNameById(old_link_a)
old_link_b_dict = self.GetPortDeviceNameById(old_link_b)
# Clean switchport connection
sql = "DELETE FROM Link WHERE porta = %d OR portb = %d" % (switch_port_id, switch_port_id)
self.db_insert(sql)
# Log message to both device
text = "Disconnected %s,%s from %s,%s" % (old_link_a_dict['device_name'], old_link_a_dict['port_name'], old_link_b_dict['device_name'], old_link_b_dict['port_name'])
self.InsertLog(self.GetObjectId(old_link_a_dict['device_name']), text)
self.InsertLog(self.GetObjectId(old_link_b_dict['device_name']), text)
# Insert new connection
sql = "INSERT INTO Link (porta,portb) VALUES (%d,%d)" % (port_id, switch_port_id)
self.db_insert(sql)
resolution = True
# Log it to both devices
device_dict = self.GetPortDeviceNameById(port_id)
switch_dict = self.GetPortDeviceNameById(switch_port_id)
text = "New connection %s,%s with %s,%s" % (device_dict['device_name'], device_dict['port_name'], switch_dict['device_name'], switch_dict['port_name'])
self.InsertLog(self.GetObjectId(device_dict['device_name']), text)
self.InsertLog(self.GetObjectId(switch_dict['device_name']), text)
else:
#Update old connection
old_switch_port_id = result[0]
if old_switch_port_id != switch_port_id:
# Clean previous link first
# Check and clean previous link (switch and port)
sql = "SELECT porta,portb FROM Link WHERE porta = %d OR portb = %d" % (switch_port_id, switch_port_id)
result = self.db_query_one(sql)
if result != None:
# Get ports id of old link
old_link_a, old_link_b = result
old_link_a_dict = self.GetPortDeviceNameById(old_link_a)
old_link_b_dict = self.GetPortDeviceNameById(old_link_b)
# Clean switchport connection
sql = "DELETE FROM Link WHERE porta = %d OR portb = %d" % (switch_port_id, switch_port_id)
self.db_insert(sql)
# Log message to both device
text = "Disconnected %s,%s from %s,%s" % (old_link_a_dict['device_name'], old_link_a_dict['port_name'], old_link_b_dict['device_name'], old_link_b_dict['port_name'])
self.InsertLog(self.GetObjectId(old_link_a_dict['device_name']), text)
self.InsertLog(self.GetObjectId(old_link_b_dict['device_name']), text)
# Insert new connection
sql = "INSERT INTO Link (porta,portb) VALUES (%d,%d)" % (switch_port_id,port_id)
self.db_insert(sql)
# Log all three devices
old_switch_dict = self.GetPortDeviceNameById(old_switch_port_id)
switch_dict = self.GetPortDeviceNameById(switch_port_id)
device_dict = self.GetPortDeviceNameById(port_id)
text = "Update connection from %s,%s to %s,%s" % (old_switch_dict['device_name'], old_switch_dict['port_name'], switch_dict['device_name'], switch_dict['port_name'])
self.InsertLog(self.GetObjectId(device_dict['device_name']), text)
text = "%s,%s changed connection from %s,%s and connected to %s,%s" % (device_dict['device_name'], device_dict['port_name'], old_switch_dict['device_name'], old_switch_dict['port_name'], switch_dict['device_name'], switch_dict['port_name'])
self.InsertLog(self.GetObjectId(old_switch_dict['device_name']), text)
self.InsertLog(self.GetObjectId(switch_dict['device_name']), text)
resolution = True
resolution = None
else:
resolution = None
else:
resolution = None
else:
resolution = None
return resolution
def InterfaceAddIpv4IP(self,object_id,device,ip):
'''Add/Update IPv4 IP on interface'''
sql = "SELECT INET_NTOA(ip) from IPv4Allocation WHERE object_id = %d AND name = '%s'" % (object_id,device)
result = self.db_query_all(sql)
if result != None:
old_ips = result
is_there = "no"
for old_ip in old_ips:
if old_ip[0] == ip:
is_there = "yes"
if is_there == "no":
sql = "SELECT name FROM IPv4Allocation WHERE object_id = %d AND ip = INET_ATON('%s')" % (object_id, ip)
result = self.db_query_all(sql)
if result != None:
if result != ():
sql = "DELETE FROM IPv4Allocation WHERE object_id = %d AND ip = INET_ATON('%s')" % (object_id, ip)
self.db_insert(sql)
self.InsertLog(object_id, "Removed IP (%s) from interface %s" % (ip, result[0][0]))
sql = "INSERT INTO IPv4Allocation (object_id,ip,name) VALUES (%d,INET_ATON('%s'),'%s')" % (object_id,ip,device)
self.db_insert(sql)
text = "Added IP %s on %s" % (ip,device)
self.InsertLog(object_id,text)
def InterfaceAddIpv6IP(self,object_id,device,ip):
'''Add/Update IPv6 IP on interface'''
#Create address object using ipaddr
addr6 = ipaddr.IPAddress(ip)
#Create IPv6 format for Mysql
ip6 = "".join(str(x) for x in addr6.exploded.split(':'))
sql = "SELECT HEX(ip) FROM IPv6Allocation WHERE object_id = %d AND name = '%s'" % (object_id, device)
result = self.db_query_all(sql)
if result != None:
old_ips = result
is_there = "no"
for old_ip in old_ips:
if old_ip[0] != ip6:
is_there = "yes"
if is_there == "no":
sql = "SELECT name FROM IPv6Allocation WHERE object_id = %d AND ip = UNHEX('%s')" % (object_id, ip)
result = self.db_query_all(sql)
if result != None:
if result != ():
sql = "DELETE FROM IPv6Allocation WHERE object_id = %d AND ip = UNHEX('%s')" % (object_id, ip)
self.db_insert(sql)
self.InsertLog(object_id, "Removed IP (%s) from interface %s" % (ip, result[0][0]))
sql = "INSERT INTO IPv6Allocation (object_id,ip,name) VALUES (%d,UNHEX('%s'),'%s')" % (object_id,ip6,device)
self.db_insert(sql)
text = "Added IPv6 IP %s on %s" % (ip,device)
self.InsertLog(object_id,text)
def InterfaceAddMAC(self,object_id,interface,mac):
'''Add MAC address to interface'''
sql = "SELECT id,name,l2address FROM Port WHERE object_id = %d AND name = '%s' AND l2address = '%s'" % (object_id, interface,mac)
result = self.db_query_one(sql)
sql1 = "SELECT id,name,l2address FROM Port WHERE object_id = %d AND name = '%s'" % (object_id, interface)
result1 = self.db_query_one(sql1)
if result == None:
if result1 != None:
if mac != result1[2]:
sql = "UPDATE Port SET l2address = '%s' WHERE object_id = %d AND name = '%s'" % (mac, object_id, interface)
self.db_insert(sql)
self.InsertLog(object_id,"Changed MAC address of %s from %s to %s" % (interface, result1[2], mac))
else:
sql = "INSERT INTO Port (object_id,name,iif_id,type,l2address) VALUES (%d,'%s',1,24,'%s')" % (object_id,interface,mac)
self.db_insert(sql)
self.InsertLog(object_id,"Added MAC address of %s as %s" % (interface, mac))
def GetDictionaryId(self,searchstring):
'''Search racktables dictionary using searchstring and return id of dictionary element'''
sql = "SELECT dict_key FROM Dictionary WHERE dict_value LIKE '%"+searchstring+"%'"
result = self.db_query_one(sql)
if result != None:
getted_id = result[0]
else:
getted_id = None
return getted_id
def CleanUnusedInterfaces(self,object_id,interface_list):
'''Remove unused old interfaces'''
sql = "SELECT id, name FROM Port WHERE object_id = %d" % (object_id)
result = self.db_query_all(sql)
# Copy interface list becouse we need to change it
interfaces = interface_list[:]
# Add drac to interface list
interfaces.append("drac")
if result != None:
for row in result:
if row[1] not in interfaces:
# Remove IPv4 allocation
sql = "DELETE FROM IPv4Allocation WHERE object_id = %d AND name = '%s'" % (object_id, row[1])
self.db_insert(sql)
self.InsertLog(object_id, "Removed IPv4 ips for %s" % row[1])
# Remove IPv6 allocation
sql = "DELETE FROM IPv6Allocation WHERE object_id = %d AND name = '%s'" % (object_id, row[1])
self.db_insert(sql)
self.InsertLog(object_id, "Removed IPv6 ips for %s" % row[1])
# Remove port links
sql = "DELETE FROM Link WHERE porta = %d OR portb = %d" % (row[0], row[0])
self.db_insert(sql)
self.InsertLog(object_id, "Remove port links %s" % row[1])
# Remove port
sql = "DELETE FROM Port WHERE object_id = %d AND name = '%s'" % (object_id, row[1])
self.db_insert(sql)
self.InsertLog(object_id, "Removed interface %s" % row[1])
def CleanVirtuals(self,object_id,virtual_servers):
'''Clean dead virtuals from hypervisor. virtual_servers is list of active virtual servers on hypervisor (object_id)'''
sql = "SELECT child_entity_id FROM EntityLink WHERE parent_entity_id = %d" % object_id
result = self.db_query_all(sql)
if result != None:
old_virtuals_ids = result
delete_virtual_id = []
new_virtuals_ids = []
# Translate names into ids
for new_virt in virtual_servers:
new_id = self.GetObjectId(new_virt)
if new_id != None:
new_virtuals_ids.append(new_id)
for old_id in old_virtuals_ids:
try:
test = new_virtuals_ids.index(old_id[0])
except ValueError:
delete_virtual_id.append(old_id[0])
if len(delete_virtual_id) != 0:
for virt_id in delete_virtual_id:
sql = "DELETE FROM EntityLink WHERE parent_entity_id = %d AND child_entity_id = %d" % (object_id,virt_id)
self.db_insert(sql)
virt_name = self.GetObjectName(virt_id)
logstring = "Removed virtual %s" % virt_name
self.InsertLog(object_id,logstring)
def CleanIPAddresses(self,object_id,ip_addresses,device):
'''Clean unused ip from object. ip addresses is list of IP addresses configured on device (device) on host (object_id)'''
sql = "SELECT INET_NTOA(ip) FROM IPv4Allocation WHERE object_id = %d AND name = '%s'" % (object_id, device)
result = self.db_query_all(sql)
if result != None:
old_ips = result
delete_ips = []
for old_ip in old_ips:
try:
test = ip_addresses.index(old_ip[0])
except ValueError:
delete_ips.append(old_ip[0])
if len(delete_ips) != 0:
for ip in delete_ips:
sql = "DELETE FROM IPv4Allocation WHERE ip = INET_ATON('%s') AND object_id = %d AND name = '%s'" % (ip,object_id,device)
self.db_insert(sql)
logstring = "Removed IP %s from %s" % (ip,device)
self.InsertLog(object_id,logstring)
def CleanIPv6Addresses(self,object_id,ip_addresses,device):
'''Clean unused ipv6 from object. ip_addresses mus be list of active IP addresses on device (device) on host (object_id)'''
sql = "SELECT HEX(ip) FROM IPv6Allocation WHERE object_id = %d AND name = '%s'" % (object_id,device)
result = self.db_query_all(sql)
if result != None:
old_ips = result
delete_ips = []
new_ip6_ips = []
#We must prepare ipv6 addresses into same format for compare
for new_ip in ip_addresses:
converted = ipaddr.IPAddress(new_ip).exploded.lower()
new_ip6_ips.append(converted)
for old_ip_hex in old_ips:
try:
#First we must construct IP from HEX
tmp = re.sub("(.{4})","\\1:", old_ip_hex[0], re.DOTALL)
#Remove last : and lower string
old_ip = tmp[:len(tmp)-1].lower()
test = new_ip6_ips.index(old_ip)
except ValueError:
delete_ips.append(old_ip)
if len(delete_ips) != 0:
for ip in delete_ips:
db_ip6_format = "".join(str(x) for x in ip.split(':'))
sql = "DELETE FROM IPv6Allocation WHERE ip = UNHEX('%s') AND object_id = %d AND name = '%s'" % (db_ip6_format,object_id,device)
self.db_insert(sql)
logstring = "Removed IP %s from %s" % (ip,device)
self.InsertLog(object_id,logstring)
def LinkVirtualHypervisor(self,object_id,virtual_id):
'''Assign virtual server to correct hypervisor'''
sql = "SELECT child_entity_id FROM EntityLink WHERE parent_entity_id = %d AND child_entity_id = %d" % (object_id,virtual_id)
result = self.db_query_one(sql)
if result == None:
sql = "INSERT INTO EntityLink (parent_entity_type, parent_entity_id, child_entity_type, child_entity_id) VALUES ('object',%d,'object',%d)" % (object_id, virtual_id)
self.db_insert(sql)
text = "Linked virtual %s with hypervisor" % self.GetObjectName(virtual_id)
self.InsertLog(object_id,text)
def AssignChassisSlot(self,chassis_name,slot_number,server_name):
'''Assign server objects to server chassis'''
chassis_id = self.GetObjectId(chassis_name)
server_id = self.GetObjectId(server_name)
slot_attribute_id = self.GetAttributeId("Slot number")
# Assign slot number to server
sql = "INSERT INTO AttributeValue (object_id,object_tid,attr_id,string_value) VALUES ( %d, 4, %d, '%s')" % ( server_id, slot_attribute_id, slot_number)
try:
self.db_insert(sql)
except:
pass
# Assign server to chassis
# Check if it's connected
sql = "SELECT parent_entity_id FROM EntityLink WHERE child_entity_type = 'object' AND child_entity_id = %d" % (server_id)
result = self.db_query_one(sql)
if result != None:
# Object is connected to someone
if result[0] != chassis_id:
# Connected to differend chassis/chassis
sql = "UPDATE EntityLink SET parent_entity_id = %d WHERE child_entity_id = %d AND child_entity_type = 'object' AND parent_entity_id = %d" % (chassis_id, server_id, result[0])
self.db_insert(sql)
old_object_name = self.GetObjectName(result[0])
self.InsertLog(old_object_name, "Unlinked server %s" % (server_name))
self.InsertLog(server_id, "Unlinked from Blade Chassis %s" % (old_object_name))
self.InsertLog(chassis_id, "Linked with server %s" % (server_name))
self.InsertLog(server_id, "Linked with Blade Chassis %s" % (chassis_name))
else:
# Object is not connected
sql = "INSERT INTO EntityLink (parent_entity_type, parent_entity_id, child_entity_type, child_entity_id) VALUES ('object', %d, 'object', %d)" % (chassis_id, server_id)
self.db_insert(sql)
self.InsertLog(chassis_id, "Linked with server %s" % (server_name))
self.InsertLog(server_id, "Linked with Blade Chassis %s" % (chassis_name))
def GetAllServerChassisId(self):
'''Get list of all server chassis IDs'''
sql = "SELECT object_id FROM AttributeValue WHERE attr_id = 2 AND uint_value = 994"
return self.db_query_all(sql)
# IPv4 Functions
def GetIP4Pools(self):
'''Get result list of IPv4 Pools'''
sql = "select INET_NTOA(ip), mask, name, comment, id from IPv4Network"
return self.db_query_all(sql)
# IPv6 Functions
def GetIP6Pools(self):
'''Get result list of IPv6 Pools'''
sql = "select HEX(ip), mask, name, comment, id from IPv6Network"
return self.db_query_all(sql)