-
Notifications
You must be signed in to change notification settings - Fork 198
/
usagestats.py
1404 lines (1213 loc) · 66.6 KB
/
usagestats.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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This is a script to calculate and compare usage statistics among different subnets.
#
# The full manual for this script can be found here:
# https://github.com/meraki/automation-scripts/blob/master/usagestats_manual.pdf
#
# To run the script, enter:
# python usagestats.py -k <key> [-d <database> -c <command> -i <initfile> -g <groups> -f <filter>] [-u <user> -p <pass> -r <recipient> -s <server>]
#
# Example:
# python usagestats.py -k 1234 -i myproject.cfg -c report:last-month
#
# Mandatory argument:
# -k <key> : Your Meraki Dashboard API key
# Database and command operation arguments:
# -d <database> : SQLite database filename. Can be omitted, if one is defined in the init config file
# A separate database filename should be used for every report project. The first time a project
# is created, its configuration should be given as an init config file, or command line arguments.
# After the project is created, a copy of its configuration is stored in the database file.
# -c <command> : Defines the operation to be executed by the script. Valid options:
# sync : Populate the database with information pulled from Dashboard. It is
# recommended that the sync operation is run at least once a week
# for every report project. The "report" command also executes a sync
# report:<time> : Generate a usage report for the time period defined in <time>. Valid
# options for <time>:
# <integer> : Report for last <integer> days
# last-week : Report for last week (Mon-Sun)
# last-month : Report for last calendar month
# <start date> to <end date> : Report between two dates, including them
# Date format: yyyy-mm-dd
# report-offline:<t> : Same as "report:", but does not execute a database sync first
# dbdump : Dump contents of database to screen
# dbreconfigure : Overwrites configuration stored in the database with a new one. Does not
# touch network or usage data. Be very careful when using this option
# If omitted, the default command is "report:last-week".
# Optional arguments to create a database:
# -i <initfile> : Init config file, containing values to arguments (see section "Writing an init config file").
# This file is only used when a new database is created.
# -g <groups> : Define groups to compare for bandwidth usage. Valid forms for <groups>:
# "<name>=<subnet>,<subnet>;<name>=<subnet>,<subnet>"
# The value <name> defines a name for the group.
# Valid values for <subnet>:
# sub:<ip address>/<mask> eg. sub:10.0.0.0/8
# vid:<VLAN ID> eg. vid:10
# vname:<VLAN name> eg. vname:Corp
# To make a subnet/vlan definition specific to one org, network or network tag prefix it
# with one or more of the following:
# @org:<org name>, @net:<net name>, @tag:<net tag>
# Leave a space ( ) between the last prefix and the subnet/vlan definition.
# Example:
# "Corp=sub:192.168.10.0/24,sub:10.10.10.0/24,vname:Corp;Guest=sub:172.16.0.0/12,@org:Myorg vid:10"
# You can use "vlanid" and "vlanname" instead of "vid" and "vname".
# If omitted, one group will be displayed, with name "Overall" and subnet "0.0.0.0/0".
# -f <filter> : Process only certain organizations, networks, network tags or device types.
# You can define multiple filters by separating them with commas. Only one filter per type
# is allowed. Available filters:
# org:<org name>
# net:<net name>
# tag:<net tag>
# dtag:<device tag>
# dtype:<device type>
# Valid options for dtype:
# dtype:mr
# dtype:ms
# dtype:mx
# dtype:all
# Option "dtype:mx" includes teleworker gateways (Zx). Example of a valid filter combination:
# "org:My company,tag:branch,dtype:mr"
# If omitted, the default filter is "dtype:mx".
# Optional arguments to send report by email. User, password and recipient are required for this function:
# -u <user> : The username (email address) that will be used to send the alert message
# -p <pass> : Password for the email address where the message is sent from
# -r <recipient> : Recipient email address
# -s <server> : Server to use for sending SMTP. If omitted, Gmail will be used
#
# Writing an init config file:
# An init config file can contain two sections: Groups and Options, as defined by the following headers:
# [GROUPS]
# [OPTIONS]
# Groups are defined as a separate section [GROUPS] with the following format:
# [GROUPS]
# groupname=First
# subnet=10.0.0.0/24
# subnet=192.168.0.0/16
# vlanid=10
# vlanname=Corp
#
# groupname=Second
# subnet=10.20.20.0/24
# @org:Orgname@net:Netname vlanid=500
# @tag:Taggednet vlanname=Guest
#
# A group is defined as a line containing the name of the group (groupname:<value>), followed by lines defining the
# subnets, VLAN IDs and VLAN names it consists of. The prefixes @org: @net: and @tag: can be used to make a line
# specific to an organization, network to network tag. You can use "vid" and "vname" instead of "vlanid" and "vlanname".
#
# The following attributes are supported under the [OPTIONS] section:
# database=<filename> : Define database filename
# filter=<filter> : Define filter
#
# Blank lines an lines beginning with a hash character (#) will be ignored.
#
# An example of an init config file can be found here:
# https://github.com/meraki/automation-scripts/blob/master/usagestats_initconfig.txt
#
# To make script chaining easier, all lines containing informational messages to the user
# start with the character @
#
# #TODO: add dbpurge?
# #TODO: add dbarchive?
# #TODO: add exclude-meraki-traffic?
# #TODO: add dbinfo?
# #TODO: check why the script is throwing warnings when the same subnet has been configured multiple times (sub+vid+vname)
import sys, getopt, requests, json, time, ipaddress, datetime, sqlite3, os.path, smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#SECTION: CLASS DEFINITIONS
class c_organizationdata:
def __init__(self):
self.name = ''
self.id = ''
self.shardhost = ''
self.nets = [] #array of c_networkdata()
#end class
class c_networkdata:
def __init__(self):
self.name = ''
self.id = ''
self.tags = ''
self.orgid = '' #used by cmdreport()
self.orgname = '' #used by cmdreport()
self.totaldown = 0 #used by cmdreport()
self.totalup = 0 #used by cmdreport()
self.devs = [] #array of c_devicedata()
self.groups = [] #c_groupdata() will be added here
#end class
class c_devicedata:
def __init__(self):
self.name = ''
self.serial = ''
self.tags = ''
self.clients = []
#end class
#class for subnet group definitions
class c_groupdata:
def __init__(self):
self.name = ''
self.id = ''
self.dbuffer = []
self.ubuffer = []
self.subnets = []
#end class
#subnet definition class. groups consist of multiples of these
class c_subnetdata:
def __init__(self):
self.subnet = ''
self.vname = ''
self.vid = ''
self.orgname = ''
self.orgid = ''
self.netname = ''
self.netid = ''
self.nettag = ''
#end class
#class that contains options as read from command line parameters or init file
class c_optiondata:
def __init__(self):
self.initfile = ''
self.dbfile = ''
self.rawcmd = ''
self.rawfilter = ''
self.rawgroups = ''
self.rawnet = ''
self.org = ''
self.netname = ''
self.nettag = ''
self.emailuser = ''
self.emailpass = ''
self.emailrecp = ''
self.emailsrvr = ''
self.sendemail = False
#end class
class c_filterdata():
def __init__(self):
self.org = ''
self.netname = ''
self.nettag = ''
self.devtag = ''
self.devtype = []
#end class
#SECTION: GLOBAL VARIABLES: MODIFY TO CHANGE SCRIPT BEHAVIOR
#Used for time.sleep(API_EXEC_DELAY). Delay added to avoid hitting dashboard API max request rate
API_EXEC_DELAY = 0.21
#connect and read timeouts for the Requests module
REQUESTS_CONNECT_TIMEOUT = 30
REQUESTS_READ_TIMEOUT = 30
#Date format string for user input
DATE_USER_FORMAT = '%Y-%m-%d'
#used to track if the database format used by this script has changed since a project database was created
DB_VERSION = 4
#SECTION: GLOBAL VARIABLES: DO NOT MODIFY
LAST_MERAKI_REQUEST = datetime.datetime.now() #used by merakirequestthrottler()
DATE_DB_FORMAT = '%Y-%m-%d' #Date format used for storage in database
#SECTION: CODE
def printusertext(p_message):
#prints a line of text that is meant for the user to read
#do not process these lines when chaining scripts
print('@ %s' % p_message)
def printhelp():
#prints help text
printusertext('This is a script to calculate and compare usage statistics among different subnets.')
printusertext('Read the manual for more information: #TODO INSERT GITHUB LINK')
printusertext('')
printusertext('To run the script, enter:')
printusertext('python usagestats.py -k <key> [-d <db> -c <cmd> -i <initf> -g <grps> -f <filtr>] [-u <usr> -p <pw> -r <rcp> -s <srv>]')
printusertext('')
printusertext('Example:')
printusertext('python usagestats.py -k 1234 -i myproject.cfg -c report:last-month')
printusertext('')
printusertext('Mandatory argument:')
printusertext(' -k <key> : Your Meraki Dashboard API key')
printusertext('Database and command operation arguments:')
printusertext(' -d <database> : SQLite database filename. Can be omitted, if one is defined in the init config file')
printusertext(' A separate database filename should be used for every report project. The first time a project')
printusertext(' is created, its configuration should be given as an init cfg file, or command line arguments.')
printusertext(' After the project is created, a copy of its configuration is stored in the database file.')
printusertext(' -c <command> : Defines the operation to be executed by the script. Valid options:')
printusertext(' sync : Populate the database with information pulled from Dashboard. It is')
printusertext(' recommended that the sync operation is run at least once a week')
printusertext(' for every report project. The "report" command also executes a sync')
printusertext(' report:<time> : Generate a usage report for the time period defined in <time>. Valid')
printusertext(' options for <time>:')
printusertext(' <integer> : Report for last <integer> days')
printusertext(' last-week : Report for last week (Mon-Sun)')
printusertext(' last-month : Report for last calendar month')
printusertext(' <start date> to <end date> : Report between two dates, including them')
printusertext(' Date format: yyyy-mm-dd')
printusertext(' report-offline:<t> : Same as "report:", but does not execute a database sync first')
printusertext(' dbdump : Dump contents of database to screen')
printusertext(' dbreconfigure : Overwrites configuration stored in the database with a new one. Doesn\'t')
printusertext(' touch network or usage data. Be very careful when using this option')
printusertext(' If omitted, the default command is "report:last-week".')
printusertext('Optional arguments to create a database:')
printusertext(' -i <initfile> : Init config file, containing values to arguments (see section "Writing an init config file").')
printusertext(' This file is only used when a new database is created.')
printusertext(' -g <groups> : Define groups to compare for bandwidth usage. Valid forms for <groups>:')
printusertext(' "<name>=<subnet>,<subnet>;<name>=<subnet>,<subnet>"')
printusertext(' The value <name> defines a name for the group.')
printusertext(' Valid values for <subnet>:')
printusertext(' sub:<ip address>/<mask> eg. sub:10.0.0.0/8')
printusertext(' vid:<VLAN ID> eg. vid:10')
printusertext(' vname:<VLAN name> eg. vname:Corp')
printusertext(' To make a subnet/vlan definition specific to one org, network or network tag prefix it')
printusertext(' with one or more of the following:')
printusertext(' @org:<org name>, @net:<net name>, @tag:<net tag>')
printusertext(' Leave a space ( ) between the last prefix and the subnet/vlan definition.')
printusertext(' Example:')
printusertext(' "Corp=sub:192.168.10.0/24,sub:10.10.10.0/24,vname:Corp;Guest=sub:172.16.0.0/12,@org:Myorg vid:10"')
printusertext(' You can use "vlanid" and "vlanname" instead of "vid" and "vname".')
printusertext(' If omitted, one group will be displayed, with name "Overall" and subnet "0.0.0.0/0".')
printusertext(' -f <filter> : Process only certain organizations, networks, network tags or device types.')
printusertext(' You can define multiple filters by separating them with commas. Only one filter per type')
printusertext(' is allowed. Available filters:')
printusertext(' org:<organization name>')
printusertext(' net:<network name>')
printusertext(' tag:<network tag>')
printusertext(' dtag:<device tag>')
printusertext(' dtype:<device type>')
printusertext(' Valid options for dtype:')
printusertext(' dtype:mr')
printusertext(' dtype:ms')
printusertext(' dtype:mx')
printusertext(' dtype:all')
printusertext(' Option "dtype:mx" includes teleworker gateways (Zx). Example of a valid filter combination:')
printusertext(' "org:My company,tag:branch,dtype:mr"')
printusertext(' If omitted, the default filter is "dtype:mx".')
printusertext('Optional arguments to send report by email. User, password and recipient are required for this function:')
printusertext(' -u <user> : The username (email address) that will be used to send the alert message')
printusertext(' -p <pass> : Password for the email address where the message is sent from')
printusertext(' -r <recipient> : Recipient email address')
printusertext(' -s <server> : Server to use for sending SMTP. If omitted, Gmail will be used')
printusertext('')
printusertext('Use double quotes ("") to pass arguments containing spaces in Windows.')
def merakirequestthrottler(p_requestcount=1):
#makes sure there is enough time between API requests to Dashboard not to hit shaper
global LAST_MERAKI_REQUEST
if (datetime.datetime.now()-LAST_MERAKI_REQUEST).total_seconds() < (API_EXEC_DELAY*p_requestcount):
time.sleep(API_EXEC_DELAY*p_requestcount)
LAST_MERAKI_REQUEST = datetime.datetime.now()
return
def getorglist(p_apikey):
#returns the organizations' list for a specified admin
merakirequestthrottler()
try:
r = requests.get('https://dashboard.meraki.com/api/v0/organizations', headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 01: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'id':'null'})
return returnvalue
rjson = r.json()
return(rjson)
def getshardhost(p_apikey, p_orgid):
#patch
return("api.meraki.com")
def getnwlist(p_apikey, p_shardhost, p_orgid):
#returns a list of all networks in an organization
#on failure returns a single record with 'null' name and id
merakirequestthrottler()
try:
r = requests.get('https://%s/api/v0/organizations/%s/networks' % (p_shardhost, p_orgid), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 03: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'name': 'null', 'id': 'null'})
return(returnvalue)
return(r.json())
def getdevicelist(p_apikey, p_shardhost, p_nwid):
#returns a list of all devices in a network
merakirequestthrottler()
try:
r = requests.get('https://%s/api/v0/networks/%s/devices' % (p_shardhost, p_nwid), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 04: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'serial': 'null', 'model': 'null'})
return(returnvalue)
return(r.json())
def getvlanlist(p_apikey, p_shardhost, p_nwid):
#returns list of all MX VLANs in a network
merakirequestthrottler()
try:
r = requests.get('https://%s/api/v0/networks/%s/vlans' % (p_shardhost, p_nwid), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 05: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'id': 'null'})
return(returnvalue)
return(r.json())
def getclientlist(p_apikey, p_shardhost, p_serial, p_timespan):
#get client list for a network device from Dashboard. No artificial delay
try:
r = requests.get('https://%s/api/v0/devices/%s/clients?timespan=%s' % (p_shardhost, p_serial, p_timespan), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 06: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'id': 'null'})
return(returnvalue)
return(r.json())
def loadinitfile(p_filename):
#Loads script options from init config file. The options will be converted to the same format that CLI arguments
#would provide them. Their values will be later processed later in another function
opt = c_optiondata()
section = ''
groupstr = ''
try:
f = open(p_filename, 'r')
except:
printusertext('ERROR 07: Unable to open file "%s"' % p_filename)
sys.exit(2)
for line in f:
stripped = line.strip()
#drop blank lines
if len(stripped) > 0:
#drop comments
if stripped[0] != '#':
if stripped == '[GROUPS]' :
section = 'grp'
elif stripped == '[OPTIONS]':
section = 'opt'
elif section == 'grp':
#groups' parsing logic goes here
splitline = stripped.split('=')
if len(splitline) < 2:
printusertext('ERROR 08: Invalid config line in file "%s": "%s"' % (p_filename, stripped))
sys.exit(2)
elif splitline[0].strip() == 'groupname':
if len(groupstr) > 0:
groupstr += ';'
groupstr += splitline[1].strip() + '='
#check only last chars of subnet/vlan label to allow for @ modifiers
elif splitline[0].strip()[-6:] == 'subnet':
if len(groupstr) > 0 and groupstr[-1:] != '=':
groupstr += ','
if len(splitline[0].strip()) > 6:
groupstr += splitline[0].strip() + ' '
groupstr += 'sub:' + splitline[1].strip()
elif splitline[0].strip()[-8:] == 'vlanname' or splitline[0].strip()[-6:] == 'vlanid' or splitline[0].strip()[-5:] == 'vname' or splitline[0].strip()[-3:] == 'vid':
if len(groupstr) > 0 and groupstr[-1:] != '=':
groupstr += ','
groupstr += splitline[0].strip() + ':' + splitline[1].strip()
elif section == 'opt':
#options' parsing logic goes here
splitline = stripped.split('=')
if len(splitline) < 2:
printusertext('ERROR 09: Invalid config line in file "%s": "%s"' % (p_filename, stripped))
sys.exit(2)
elif splitline[0].strip() == 'database':
opt.dbfile = splitline[1].strip()
elif splitline[0].strip() == 'filter':
opt.rawfilter = splitline[1].strip()
f.close()
opt.rawgroups = groupstr
return(opt)
def decodegroups(p_groupstr, p_dbfile):
#converts a groups' definition string into an object structure
grp = []
gcount = 0
splitstr = p_groupstr.split(';')
for block in splitstr:
grp.append(c_groupdata())
scount = 0 # subnet count in this group
splitblock = block.split('=')
grp[gcount].name = splitblock[0].strip()
grp[gcount].id = str(gcount)
if len(splitblock) > 1:
payload = splitblock[1].split(',')
else:
printusertext('ERROR 10: Invalid group definition "%s"' % block.strip())
sys.exit(2)
for net in payload:
grp[gcount].subnets.append(c_subnetdata())
rest = net.strip()
flag_gotmod = False
prevcursor = 0
rcursor = 0
rcursor = rest.find('@')
flag_gotmod = rcursor != -1
flag_gotsubnet = True
colonptr = rest.rfind(':')
if rest[colonptr-6:colonptr] == 'vlanid':
grp[gcount].subnets[scount].vid = rest[colonptr+1:].strip()
rest = rest[:colonptr-6]
#check to see if script read anything other than blank space
flag_gotsubnet = len(grp[gcount].subnets[scount].vid) > 0
elif rest[colonptr-3:colonptr] == 'vid':
grp[gcount].subnets[scount].vid = rest[colonptr+1:].strip()
rest = rest[:colonptr-3]
flag_gotsubnet = len(grp[gcount].subnets[scount].vid) > 0
elif rest[colonptr-8:colonptr] == 'vlanname':
grp[gcount].subnets[scount].vname = rest[colonptr+1:].strip()
rest = rest[:colonptr-8]
flag_gotsubnet = len(grp[gcount].subnets[scount].vname) > 0
elif rest[colonptr-5:colonptr] == 'vname':
grp[gcount].subnets[scount].vname = rest[colonptr+1:].strip()
rest = rest[:colonptr-5]
flag_gotsubnet = len(grp[gcount].subnets[scount].vname) > 0
elif rest[colonptr-3:colonptr] == 'sub':
grp[gcount].subnets[scount].subnet = rest[colonptr+1:].strip()
rest = rest[:colonptr-3]
flag_gotsubnet = len(grp[gcount].subnets[scount].subnet) > 0
else:
flag_gotsubnet = False
if not flag_gotsubnet:
printusertext('ERROR 11: Invalid subnet definition "%s"' % net.strip())
sys.exit(2)
while flag_gotmod:
prevcursor = rcursor
rcursor = rest[1:].find('@')
if rcursor > -1:
smod = rest[1:rcursor+1].split(':')
else:
smod = rest[1:].split(':')
flag_gotmod = False
flag_modparsefail = False
if len(smod) > 1:
modlabel = smod[0].strip()
if modlabel == 'org':
if grp[gcount].subnets[scount].orgname == '':
grp[gcount].subnets[scount].orgname = smod[1].strip()
else:
flag_modparsefail = True
elif modlabel == 'net':
if grp[gcount].subnets[scount].netname == '':
grp[gcount].subnets[scount].netname = smod[1].strip()
else:
flag_modparsefail = True
elif modlabel == 'tag':
if grp[gcount].subnets[scount].nettag == '':
grp[gcount].subnets[scount].nettag = smod[1].strip()
else:
flag_modparsefail = True
else:
flag_modparsefail = True
if flag_modparsefail:
printusertext('ERROR 12: Invalid subnet definition %s' % net.strip())
sys.exit(2)
rest = rest[rcursor+1:].strip()
scount += 1
gcount += 1
#if the function made it through here, it is safe to say that the group string is clean,
#so it can be chopped up and stored in the database
try:
db = sqlite3.connect(p_dbfile)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS
groups(id INTEGER PRIMARY KEY, groupid INTEGER, groupname TEXT, subnets TEXT)''')
db.commit()
cursor.execute('''SELECT groupid FROM groups''')
data = cursor.fetchall()
if len(data) == 0:
for i in range (0, len(grp)):
splitgroup = splitstr[i].split('=')
cursor.execute( '''INSERT INTO groups(groupid, groupname, subnets)
VALUES(?,?,?)''', ( i,splitgroup[0].strip(),splitgroup[1].strip() ) )
db.commit()
db.close()
except:
printusertext('ERROR 13: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
return (grp)
def buildorgstructure(p_apikey, p_filters):
#builds master object where all org, net, device and client data will be read
orgs = []
printusertext('INFO: Retrieving organization info')
#compile list of organizations to be processed
orgjson = getorglist(p_apikey)
if orgjson[0]['id'] == 'null':
printusertext('ERROR 14: Unable to retrieve org list')
sys.exit(2)
i = 0
for record in orgjson:
if p_filters.org == '' or record['name'] == p_filters.org:
orgs.append(c_organizationdata())
orgs[i].name = record['name']
orgs[i].id = record['id']
i += 1
#get shard host/FQDN where destination org is stored
#this call sometimes fails. implementing a try-verify-wait-repeat loop
MAX_SHARD_RESOLVE_TRIES = 10
for record in orgs:
flag_unabletoresolveshard = True
for i in range (0, MAX_SHARD_RESOLVE_TRIES):
shardhost = getshardhost(p_apikey, record.id)
if shardhost == 'null':
time.sleep(API_EXEC_DELAY*(i+1))
else:
flag_unabletoresolveshard = False
break
if flag_unabletoresolveshard:
printusertext('ERROR 15: Unable to read data for org "%s"' % record.name)
sys.exit(2)
else:
record.shardhost = shardhost
#compile list of networks and devices to be processed
for org in orgs:
netcount = 0
netbuffer = getnwlist(p_apikey, org.shardhost, org.id)
if len(netbuffer) > 0:
if netbuffer[0]['id'] != 'null':
for net in netbuffer:
if p_filters.netname == '' or p_filters.netname == net['name']:
if p_filters.nettag == '' or net['tags'].find(p_filters.nettag) > -1:
org.nets.append(c_networkdata())
org.nets[netcount].name = net['name']
org.nets[netcount].id = net['id']
org.nets[netcount].tags = net['tags']
devcount = 0
devbuffer = getdevicelist(p_apikey, org.shardhost, net['id'])
if len(devbuffer) > 0:
if devbuffer[0]['serial'] != 'null':
for dev in devbuffer:
flag_matchdevtype = False
for dtype in p_filters.devtype:
if dev['model'][:len(dtype)] == dtype:
flag_matchdevtype = True
break
if flag_matchdevtype:
#match devtag. tags might not exist
flag_passdevtagtest = False
if p_filters.devtag == '': #if no devtag filter, nothing to check
flag_passdevtagtest = True
elif 'tags' in dev: #avoid invalid reference crash
if dev['tags'].find(p_filters.devtag) > -1:
flag_passdevtagtest = True
#(else fail test)
if flag_passdevtagtest:
org.nets[netcount].devs.append(c_devicedata())
if not dev['name'] is None:
org.nets[netcount].devs[devcount].name = dev['name']
org.nets[netcount].devs[devcount].serial = dev['serial']
if 'tags' in dev:
org.nets[netcount].devs[devcount].tags = dev['tags']
devcount += 1
else:
printusertext('WARNING: Unable to read device data for net "%s"' % net['name'])
else:
printusertext('INFO: Network "%s" contains no devices' % net['name'])
netcount += 1
else:
printusertext('WARNING: Unable to read network data for org "%s"' % org.name)
else:
printusertext('INFO: Organization "%s" contains no networks' % org.name)
#remove orgs and nets that contain no devices
cleanorgs = []
orgcount = 0
for org in orgs:
netcount = 0
flag_neworg = True
for net in org.nets:
devcount = 0
flag_newnet = True
for dev in net.devs:
if flag_newnet:
if flag_neworg:
cleanorgs.append(c_organizationdata())
cleanorgs[orgcount].name = org.name
cleanorgs[orgcount].id = org.id
cleanorgs[orgcount].shardhost = org.shardhost
orgcount += 1
flag_neworg = False
cleanorgs[orgcount-1].nets.append(c_networkdata())
cleanorgs[orgcount-1].nets[netcount].name = net.name
cleanorgs[orgcount-1].nets[netcount].id = net.id
cleanorgs[orgcount-1].nets[netcount].tags = net.tags
netcount += 1
flag_newnet = False
cleanorgs[orgcount-1].nets[netcount-1].devs.append(c_devicedata())
cleanorgs[orgcount-1].nets[netcount-1].devs[devcount].name = dev.name
cleanorgs[orgcount-1].nets[netcount-1].devs[devcount].serial = dev.serial
cleanorgs[orgcount-1].nets[netcount-1].devs[devcount].tags = dev.tags
devcount += 1
return (cleanorgs)
def decodefilters(p_filterstr):
#extract individual filter components from raw filter string
output = c_filterdata()
splitstr = p_filterstr.split(',')
try:
for item in splitstr:
splititem = item.split(':')
if len(splititem) > 1:
label = splititem[0].strip().lower()
if label == 'org':
if output.org == '':
output.org = splititem[1].strip()
else:
raise ValueError('org')
elif label == 'net':
if output.netname == '':
output.netname = splititem[1].strip()
else:
raise ValueError('net')
elif label == 'tag':
if output.nettag == '':
output.nettag = splititem[1].strip()
else:
raise ValueError('tag')
elif label == 'dtype':
if len(output.devtype) == 0:
value = splititem[1].strip().lower()
if value == 'mr':
output.devtype.append('MR')
if value == 'ms':
output.devtype.append('MS')
if value == 'mx':
output.devtype.append('MX')
output.devtype.append('Z')
if value == 'all':
output.devtype.append('MR')
output.devtype.append('MS')
output.devtype.append('MX')
output.devtype.append('Z')
else:
raise ValueError('dtype')
elif label == 'dtag':
if output.devtag == '':
output.devtag = splititem[1].strip()
else:
raise ValueError('dtag')
else:
raise ValueError('label')
except:
printusertext('ERROR 16: Invalid filter combination "%s"' % p_filterstr)
sys.exit(2)
if len(output.devtype) == 0:
output.devtype.append('MX')
output.devtype.append('Z')
return(output)
def cmdsyncdatabase(p_apikey, p_orgs, p_dbfile):
#pulls VLAN usage data from Dashboard to local SQLite database
printusertext('INFO: Starting database sync. Please be patient')
#create network ids to names mapping table if needed
try:
db = sqlite3.connect(p_dbfile)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS
networks(id INTEGER PRIMARY KEY, netid TEXT, netname TEXT, netorgid TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS
organizations(id INTEGER PRIMARY KEY, orgid TEXT, orgname TEXT)''')
db.commit()
except:
printusertext('ERROR 17: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
for org in p_orgs:
printusertext('INFO: Processing organization "%s"' % org.name)
try:
cursor.execute('''SELECT orgid FROM organizations WHERE orgid=?''', (org.id,))
data = cursor.fetchall()
if len(data) == 0:
cursor.execute('''INSERT INTO organizations(orgid, orgname) VALUES(?,?)''', (org.id,org.name))
db.commit()
except:
printusertext('ERROR 18: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
for net in org.nets:
printusertext('INFO: Processing network "%s"...' % net.name)
#make sure network name to id mapping and data table exist
try:
cursor.execute('''SELECT netid FROM networks WHERE netid=?''', (net.id,))
data = cursor.fetchall()
if len(data) == 0:
cursor.execute('''CREATE TABLE IF NOT EXISTS
data_''' + net.id + '''(id INTEGER PRIMARY KEY, date TEXT, groupid TEXT, up TEXT, down TEXT)''')
db.commit()
cursor.execute('''INSERT INTO
networks(netid, netname, netorgid) VALUES(?,?,?)''', (net.id,net.name,org.id))
db.commit()
except:
printusertext('ERROR 19: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
today = datetime.datetime.combine(datetime.datetime.now().date(), datetime.time(0,0,0))
max_past_date = today - datetime.timedelta(days=29)
#find newest data entry
try:
cursor.execute('''SELECT date FROM data_''' + net.id + ''' ORDER BY date DESC''')
data = cursor.fetchone()
if data is None:
newestdate = max_past_date
else:
newestdate = datetime.datetime.strptime(data[0], DATE_DB_FORMAT)
if (newestdate < max_past_date):
newestdate = max_past_date
except:
printusertext('ERROR 20: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
dcount = (today - newestdate).days #cannot be more than 29 days and contains no time
for group in net.groups:
for i in range(0,dcount):
group.dbuffer.append(0.0)
group.ubuffer.append(0.0)
if dcount > 1:
for dev in net.devs:
#preload first buffers with client usage data to simplify main loop
startdate = today - datetime.timedelta(days=dcount-1)
merakirequestthrottler()
dstart = int((datetime.datetime.now()-startdate).total_seconds())
clientsafter = getclientlist(p_apikey, org.shardhost, dev.serial, str(dstart))
for client in clientsafter:
flag_gotmatch = False
for group in net.groups:
for subnet in group.subnets:
if (subnet.subnet != '' and ipaddress.IPv4Address(client['ip']) in ipaddress.IPv4Network(subnet.subnet)) or (subnet.vid != '' and (client['vlan'] == int(subnet.vid))):
group.dbuffer[0] += client['usage']['sent'] #values returned by API are reverse
group.ubuffer[0] += client['usage']['recv']
flag_gotmatch = True
break
if flag_gotmatch:
break
#main loop: for every day: get client data and add it up. more processing later
for i in range(1, dcount):
startdate = today - datetime.timedelta(days=dcount-i)
merakirequestthrottler()
dend = int((datetime.datetime.now()-startdate).total_seconds()) - 86400
clientsafter = getclientlist(p_apikey, org.shardhost, dev.serial, str(dend))
for client in clientsafter:
flag_gotmatch = False
for group in net.groups:
for subnet in group.subnets:
if (subnet.subnet != '' and ipaddress.IPv4Address(client['ip']) in ipaddress.IPv4Network(subnet.subnet)) or (subnet.vid != '' and (client['vlan'] == int(subnet.vid))):
group.dbuffer[i] += client['usage']['sent'] #values returned by API are reverse
group.ubuffer[i] += client['usage']['recv']
flag_gotmatch = True
break
if flag_gotmatch:
break
#end "for dev in net.devs"
#calculate daily group usage and write to database
for i in range (1, dcount):
for group in net.groups:
try:
cursor.execute('''INSERT INTO data_''' + net.id + '''(date, groupid, up, down)
VALUES(?,?,?,?)''', ((today - datetime.timedelta(days=dcount-i)).date().isoformat(), group.id, str(int(group.dbuffer[i-1]-group.dbuffer[i])), str(int(group.ubuffer[i-1]-group.ubuffer[i]))))
except:
printusertext('ERROR 21: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
try:
db.commit()
except:
printusertext('ERROR 22: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
#end "if dcount > 0"
#end "for net in org.nets"
#end "for org in orgs"
try:
db.close()
except:
printusertext('ERROR 23: Unable to connect to database file "%s"' % p_dbfile)
sys.exit(2)
printusertext('INFO: Database sync complete')
return (0)
def cmddatabasedump(p_opt):
#dumps contents of database to screen
if os.path.exists(p_opt.dbfile):
try:
db = sqlite3.connect(p_opt.dbfile)
cursor = db.cursor()
cursor.execute('''SELECT name FROM sqlite_master WHERE type='table' ''')
data = cursor.fetchall()
if len(data) != 0:
for record in data:
print (' ---')
print ('TABLE %s' % record[0])
execstring = '''SELECT * FROM ''' + record[0]
if record[0][:4] == 'data':
execstring += ''' ORDER BY date DESC'''
cursor.execute(execstring)
recdata = cursor.fetchall()
for item in recdata:
print (item)
db.close()
except:
printusertext('ERROR 24: Unable to connect to database file "%s"' % p_opt.dbfile)
sys.exit(2)
else:
printusertext('ERROR 25: File "%s" does not exist' % p_opt.dbfile)
sys.exit(2)
return (0)
def cmdreport(p_opt):
#creates reports according to user preferences
#TODO: add warning for missing data
splitcmd = p_opt.rawcmd.split(':')
firstpart = splitcmd[0].strip().lower()
if (firstpart != 'report' and firstpart != 'report-offline') or len(splitcmd) != 2:
printusertext('ERROR 26: Invalid command syntax "%s"' % p_opt.rawcmd)
sys.exit(2)
#parse time definition parameters and set start and end dates accordingly
timedef = splitcmd[1].strip().lower()
if timedef == 'last-week':
todaydate = datetime.datetime.combine(datetime.datetime.now().date(), datetime.time(0,0,0))
weekdaytoday = todaydate.date().weekday()
#last and first day of previous week
enddate = (todaydate - datetime.timedelta(days=weekdaytoday+1) )
startdate = enddate - datetime.timedelta(6)
elif timedef == 'last-month':
todaydate = datetime.datetime.combine(datetime.datetime.now().date(), datetime.time(0,0,0))
#last and first day of previous month
enddate = todaydate - datetime.timedelta(days=int(todaydate.strftime('%d')))
startdate = enddate - datetime.timedelta(days=int(enddate.strftime('%d'))-1)
else:
dates = timedef.split('to')
if len(dates) == 2:
try:
date1 = datetime.datetime.strptime(dates[0].strip(), DATE_USER_FORMAT)
date2 = datetime.datetime.strptime(dates[1].strip(), DATE_USER_FORMAT)
except:
printusertext('ERROR 27: Invalid time range definition "%s"' % timedef)
sys.exit(2)
if date1 < date2:
startdate = date1
enddate = date2
else:
startdate = date2