-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
1510 lines (1328 loc) · 63.8 KB
/
setup.php
File metadata and controls
1510 lines (1328 loc) · 63.8 KB
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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2007 The Cacti Group |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
include_once($config['base_path'] . '/plugins/extenddb/ssh2.php');
include_once($config['base_path'] . '/plugins/extenddb/telnet.php');
include_once($config['base_path'] . '/lib/ping.php');
require_once($config['base_path'] . '/lib/api_automation_tools.php');
require_once($config['base_path'] . '/lib/api_automation.php');
require_once($config['base_path'] . '/lib/api_data_source.php');
require_once($config['base_path'] . '/lib/api_graph.php');
require_once($config['base_path'] . '/lib/api_device.php');
require_once($config['base_path'] . '/lib/api_tree.php');
require_once($config['base_path'] . '/lib/data_query.php');
require_once($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/snmp.php');
require_once($config['base_path'] . '/lib/sort.php');
require_once($config['base_path'] . '/lib/template.php');
function plugin_extenddb_install() {
global $config;
api_plugin_register_hook('extenddb', 'config_settings', 'extenddb_config_settings', 'setup.php');
api_plugin_register_hook('extenddb', 'config_form', 'extenddb_config_form', 'setup.php');
api_plugin_register_hook('extenddb', 'api_device_new', 'extenddb_api_device_new', 'setup.php');
// utilities action
api_plugin_register_hook('extenddb', 'utilities_action', 'extenddb_utilities_action', 'setup.php');
api_plugin_register_hook('extenddb', 'utilities_list', 'extenddb_utilities_list', 'setup.php');
// Device action
api_plugin_register_hook('extenddb', 'device_action_array', 'extenddb_device_action_array', 'setup.php');
api_plugin_register_hook('extenddb', 'device_action_execute', 'extenddb_device_action_execute', 'setup.php');
api_plugin_register_hook('extenddb', 'device_action_prepare', 'extenddb_device_action_prepare', 'setup.php');
api_plugin_register_hook('extenddb', 'device_remove', 'extenddb_api_device_remove', 'setup.php');
// add new filter for device
api_plugin_register_hook('extenddb', 'device_filters', 'extenddb_device_filters', 'setup.php');
api_plugin_register_hook('extenddb', 'device_sql_where', 'extenddb_device_sql_where', 'setup.php');
api_plugin_register_hook('extenddb', 'device_table_bottom', 'extenddb_device_table_bottom', 'setup.php');
// host edit form, used to fill the model and serial number, or allow to enter one (same format of the extenddb model and serial no)
api_plugin_register_hook('extenddb', 'host_edit_bottom', 'extenddb_host_edit_bottom', 'setup.php');
extenddb_setup_table(); // setup the table if new install
fill_model_db(); // place where new device are added
}
function plugin_extenddb_uninstall () {
// Do any extra Uninstall stuff here
// Remove items from the settings table
db_execute('ALTER TABLE host DROP COLUMN isPhone');
db_execute('ALTER TABLE host DROP COLUMN isWifi');
db_execute('DROP TABLE plugin_extenddb_host_model');
db_execute('DROP TABLE plugin_extenddb_host_serial_no');
}
function plugin_extenddb_check_config () {
// Here we will check to ensure everything is configured
extenddb_check_upgrade();
return true;
}
function plugin_extenddb_upgrade () {
// Here we will upgrade to the newest version
extenddb_check_upgrade();
return false;
}
function extenddb_check_upgrade() {
global $config;
$version = plugin_extenddb_version ();
$current = $version['version'];
$old = db_fetch_cell('SELECT version
FROM plugin_config
WHERE directory="extenddb"');
if ($current != $old) {
// Set the new version
db_execute("UPDATE plugin_config SET version='$current' WHERE directory='extenddb'");
db_execute("UPDATE plugin_config SET
version='" . $version['version'] . "',
name='" . $version['longname'] . "',
author='" . $version['author'] . "',
webpage='" . $version['homepage'] . "'
WHERE directory='" . $version['name'] . "' ");
if( $old < '1.1' ) {
api_plugin_db_add_column ('extenddb', 'host', array('name' => 'serial_no', 'type' => 'char(50)', 'NULL' => true, 'default' => ''));
api_plugin_db_add_column ('extenddb', 'host', array('name' => 'type', 'type' => 'char(50)', 'NULL' => true, 'default' => ''));
}
if( $old < '1.1.2' ) {
api_plugin_db_add_column ('extenddb', 'host', array('name' => 'isPhone', 'type' => 'char(2)', 'NULL' => true, 'default' => ''));
}
if( $old < '1.2.3' ) {
// Device action
api_plugin_register_hook('extenddb', 'device_action_array', 'extenddb_device_action_array', 'setup.php');
api_plugin_register_hook('extenddb', 'device_action_execute', 'extenddb_device_action_execute', 'setup.php');
api_plugin_register_hook('extenddb', 'device_action_prepare', 'extenddb_device_action_prepare', 'setup.php');
}
if( $old < '1.3.2' ) {
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'mediumint(8)', 'auto_increment'=>'');
$data['columns'][] = array('name' => 'snmp_SysObjectId', 'type' => 'varchar(64)', 'NULL' => false );
$data['columns'][] = array('name' => 'oid_model', 'type' => 'varchar(64)', 'NULL' => false );
$data['columns'][] = array('name' => 'oid_sn', 'type' => 'varchar(64)', 'NULL' => false );
$data['columns'][] = array('name' => 'model', 'type' => 'varchar(64)', 'NULL' => false );
$data['primary'] = 'id';
$data['keys'][] = array('name' => 'snmp_SysObjectId', 'columns' => 'snmp_SysObjectId');
$data['keys'][] = array('name' => 'oid_model', 'columns' => 'oid_model');
$data['keys'][] = array('name' => 'model', 'columns' => 'model');
$data['type'] = 'InnoDB';
api_plugin_db_table_create('extenddb', 'plugin_extenddb_model', $data);
}
if( $old < '1.3.4' ) {
}
if( $old < '1.3.5' ) {
// change the name of the switch type to model, to be more coherent between plugin
db_execute('ALTER TABLE host CHANGE type model CHAR(50)');
}
if( $old < '1.3.6' ) {
api_plugin_register_hook('extenddb', 'device_filters', 'extenddb_device_filters', 'setup.php');
api_plugin_register_hook('extenddb', 'device_sql_where', 'extenddb_device_sql_where', 'setup.php');
api_plugin_register_hook('extenddb', 'device_table_bottom', 'extenddb_device_table_bottom', 'setup.php');
}
if( $old < '1.4.0' ) {
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'mediumint(8)', 'auto_increment'=>'');
$data['columns'][] = array('name' => 'host_id', 'type' => 'mediumint(8)', 'NULL' => false );
$data['columns'][] = array('name' => 'serial_no', 'type' => 'char(50)', 'NULL' => true );
$data['primary'] = "id`,`host_id";
$data['keys'][] = array('name' => 'host_id', 'columns' => 'host_id');
$data['keys'][] = array('name' => 'serial_no', 'columns' => 'serial_no');
$data['type'] = 'InnoDB';
api_plugin_db_table_create('extenddb', 'plugin_extenddb_host_serial_no', $data);
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'mediumint(8)', 'auto_increment'=>'');
$data['columns'][] = array('name' => 'host_id', 'type' => 'mediumint(8)', 'NULL' => false );
$data['columns'][] = array('name' => 'model', 'type' => 'char(50)', 'NULL' => true );
$data['primary'] = "id`,`host_id";
$data['keys'][] = array('name' => 'host_id', 'columns' => 'host_id');
$data['keys'][] = array('name' => 'model', 'columns' => 'model');
$data['type'] = 'InnoDB';
api_plugin_db_table_create('extenddb', 'plugin_extenddb_host_model', $data);
// upgrade from previous 1.4.0, so transfert data to new table
$sql_query = "SELECT id, model, serial_no FROM host WHERE ((serial_no != '' AND serial_no IS NOT NULL) OR (model IS NOT NULL AND model != '')) AND snmp_version > 0";
$dbquery = db_fetch_assoc( $sql_query );
// be sure than the query return something
if( count($dbquery) > 0 ) {
foreach( $dbquery as $hostid ){
$explode_model = explode('|', $hostid['model']);
foreach( $explode_model as $key => $model ) {
db_execute_prepared('INSERT INTO plugin_extenddb_host_model (id, host_id, model) VALUES (?, ?, ?)', array($key,$hostid['id'], $model) );
}
$explode_serial = explode('|', $hostid['serial_no']);
foreach( $explode_serial as $key => $serial ) {
db_execute_prepared('INSERT INTO plugin_extenddb_host_serial_no (id, host_id, serial_no) VALUES (?, ?, ?)', array($key, $hostid['id'], $serial) );
}
}
db_remove_column('host', 'model');
db_remove_column('host', 'serial_no');
}
api_plugin_register_hook('extenddb', 'device_remove', 'extenddb_api_device_remove', 'setup.php');
}
if( $old < '1.4.1' ) {
api_plugin_db_add_column ('extenddb', 'host', array('name' => 'isWifi', 'type' => 'char(2)', 'NULL' => true, 'default' => ''));
}
if( $old < '1.4.2' ) {
api_plugin_register_hook('extenddb', 'host_edit_bottom', 'extenddb_host_edit_bottom', 'setup.php');
}
}
}
function plugin_extenddb_version () {
global $config;
$info = parse_ini_file($config['base_path'] . '/plugins/extenddb/INFO', true);
return $info['info'];
}
function extenddb_setup_table() {
global $config;
include_once($config["library_path"] . "/database.php");
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'mediumint(8)', 'NULL' => false);
$data['columns'][] = array('name' => 'host_id', 'type' => 'mediumint(8)', 'NULL' => false );
$data['columns'][] = array('name' => 'serial_no', 'type' => 'char(50)', 'NULL' => true );
$data['primary'] = "id`,`host_id";
$data['keys'][] = array('name' => 'host_id', 'columns' => 'host_id');
$data['keys'][] = array('name' => 'serial_no', 'columns' => 'serial_no');
$data['type'] = 'InnoDB';
api_plugin_db_table_create('extenddb', 'plugin_extenddb_host_serial_no', $data);
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'mediumint(8)', 'NULL' => false);
$data['columns'][] = array('name' => 'host_id', 'type' => 'mediumint(8)', 'NULL' => false );
$data['columns'][] = array('name' => 'model', 'type' => 'char(50)', 'NULL' => true );
$data['primary'] = "id`,`host_id";
$data['keys'][] = array('name' => 'host_id', 'columns' => 'host_id');
$data['keys'][] = array('name' => 'model', 'columns' => 'model');
$data['type'] = 'InnoDB';
api_plugin_db_table_create('extenddb', 'plugin_extenddb_host_model', $data);
$data = array();
$data['columns'][] = array('name' => 'id', 'type' => 'mediumint(8)', 'auto_increment'=>'');
$data['columns'][] = array('name' => 'snmp_SysObjectId', 'type' => 'varchar(64)', 'NULL' => false );
$data['columns'][] = array('name' => 'oid_model', 'type' => 'varchar(64)', 'NULL' => false );
$data['columns'][] = array('name' => 'oid_sn', 'type' => 'varchar(64)', 'NULL' => false );
$data['columns'][] = array('name' => 'model', 'type' => 'varchar(64)', 'NULL' => false );
$data['primary'] = 'id';
$data['keys'][] = array('name' => 'snmp_SysObjectId', 'columns' => 'snmp_SysObjectId');
$data['keys'][] = array('name' => 'oid_model', 'columns' => 'oid_model');
$data['keys'][] = array('name' => 'model', 'columns' => 'model');
$data['type'] = 'InnoDB';
api_plugin_db_table_create('extenddb', 'plugin_extenddb_model', $data);
api_plugin_db_add_column ('extenddb', 'host', array('name' => 'isWifi', 'type' => 'char(2)', 'NULL' => true, 'default' => ''));
api_plugin_db_add_column ('extenddb', 'host', array('name' => 'isPhone', 'type' => 'char(2)', 'NULL' => true, 'default' => ''));
}
function extenddb_api_device_remove( $host_id ){
global $asset_status;
extdb_log('extenddb_api_device_remove Start: '.print_r($host_id, true) );
foreach( $host_id as $host ) {
extdb_log('extenddb_api_device_remove remove from serial_no table: '.print_r($host, true) );
db_execute_prepared('DELETE FROM plugin_extenddb_host_serial_no where host_id=?', array($host) );
extdb_log('extenddb_api_device_remove remove from model table: '.print_r($host_id, true) );
db_execute_prepared('DELETE FROM plugin_extenddb_host_model where host_id=?', array($host) );
}
return $host_id;
}
function extenddb_config_form () {
// 'value' => db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($host_id)),
global $fields_host_edit;
$fields_host_edit2 = $fields_host_edit;
$fields_host_edit3 = array();
foreach ($fields_host_edit2 as $f => $a) {
$fields_host_edit3[$f] = $a;
if ($f == 'external_id') {
$fields_host_edit3['extenddb_header'] = array(
'friendly_name' => __('Extend DB'),
'method' => 'spacer',
'collapsible' => 'true'
);
$fields_host_edit3['serial_no'] = array(
'method' => 'drop_sql',
'friendly_name' => 'Serial No',
'description' => 'Enter the serial number.',
'max_length' => 50,
'value' => '|arg1:serial_no|',
'sql' => 'SELECT id, serial_no AS name FROM plugin_extenddb_host_serial_no WHERE host_id="|arg1:id|"',
'default' => '',
);
$fields_host_edit3['model'] = array(
'friendly_name' => 'Model',
'description' => 'This is the model of equipement.',
'method' => 'drop_sql',
'max_length' => 50,
'sql' => 'SELECT id, model AS name FROM plugin_extenddb_host_model WHERE host_id="|arg1:id|"',
'value' => '|arg1:model|',
'default' => '',
);
$fields_host_edit3['isPhone'] = array(
'friendly_name' => 'isPhone',
'description' => 'Is it a phone ?',
'method' => 'checkbox',
'value' => '|arg1:isPhone|',
'default' => '',
);
$fields_host_edit3['isWifi'] = array(
'friendly_name' => 'isWifi',
'description' => 'Is it a WiFi AP ?',
'method' => 'checkbox',
'value' => '|arg1:isWifi|',
'default' => '',
);
}
}
$fields_host_edit = $fields_host_edit3;
}
function extenddb_utilities_list () {
global $colors, $config;
html_header(array("extenddb Plugin"), 4);
form_alternate_row();
?>
<td class="textArea">
<a href='utilities.php?action=extenddb_complete'>Complete Serial Number and Model</a>
</td>
<td class="textArea">
Complete Serial Number anb Model of all non filed device
</td>
<?php
form_end_row();
form_alternate_row();
?>
<td class="textArea">
<a href='utilities.php?action=extenddb_rebuild'>Recheck All Cisco Device</a>
</td>
<td class="textArea">
Build Serial Number anb Model of All Cisco Device
</td>
<?php
form_end_row();
form_alternate_row();
?>
<td class="textArea">
<a href='<?php print $config['url_path'] . 'plugins/extenddb/'?>extenddb_type.php?action=display_model_db'>Edit the ExtendDB table</a>
</td>
<td class="textArea">
Change, add or remove a model entry on the ExtendDB table
</td>
<?php
form_end_row();
form_alternate_row();
?>
<td class="textArea">
<a href='utilities.php?action=extenddb_purge_wifi'>Remove inexistant WiFi devices</a>
</td>
<td class="textArea">
Purge the DB with WiFi devices that are not connected anymore
</td>
<?php
form_end_row();
form_alternate_row();
?>
<td class="textArea">
<a href='utilities.php?action=extenddb_purge'>Remove inexistant devices</a>
</td>
<td class="textArea">
Purge the DB with devices that are not connected anymore
</td>
<?php
form_end_row();
form_alternate_row();
?>
<td class="textArea">
<a href='utilities.php?action=extenddb_count'>ExtendDB model count</a>
</td>
<td class="textArea">
Count the number of each device model
</td>
<?php
form_end_row();
form_alternate_row();
?>
<td class="textArea">
<a href='utilities.php?action=extenddb_export_model_SN'>ExtendDB Export Model and SN</a>
</td>
<td class="textArea">
Export in CSV format, the model and SN of all active device
</td>
<?php
form_end_row();
}
function extenddb_utilities_action ($action) {
global $config, $item_rows;
cacti_log( 'Extenddb utilities start: '.$action, false, "EXTENDDB" ); // Log somme information on cactilog
if ($action == 'extenddb_complete') {
// get device list, where serial number is empty, or model
$dbquery = db_fetch_assoc("SELECT * FROM host
LEFT JOIN plugin_extenddb_host_serial_no AS pehs ON pehs.host_id=host.id
LEFT JOIN plugin_extenddb_host_model AS pehm ON pehm.host_id=host.id
WHERE (pehs.serial_no IS NULL OR pehs.serial_no = '' OR pehm.model IS NULL OR pehm.model ='')
AND host.status = '3' AND host.disabled != 'on'
AND host.snmp_sysDescr LIKE '%cisco%'
AND host.snmp_version>0
ORDER BY host.id");
// Upgrade the extenddb value
if( $dbquery > 0 ) {
foreach ($dbquery as $host) {
update_sn_model( $host );
}
}
top_header();
utilities();
bottom_footer();
} elseif ($action == 'extenddb_rebuild') {
// get device list
$dbquery = db_fetch_assoc("SELECT * FROM host
WHERE status = '3' AND disabled != 'on'
AND snmp_sysDescr LIKE '%cisco%'
AND snmp_version>0
ORDER BY id");
// Upgrade the extenddb value
if( $dbquery > 0 ) {
foreach ($dbquery as $host) {
update_sn_model( $host );
}
}
top_header();
utilities();
bottom_footer();
} elseif ($action=='extenddb_purge_wifi' ) {
// purge the device that are not pingable
// get device list
$dbquery = "SELECT * FROM host WHERE disabled='on'
AND availability > 0
AND isWifi = 'on'
ORDER BY id";
$extenddb_purge_wifi = db_fetch_assoc($dbquery);
extdb_log('extenddb_utilities_action nb device: '. count($extenddb_purge_wifi) );
if( $extenddb_purge_wifi > 0 ) {
foreach ($extenddb_purge_wifi as $host) {
// create new ping socket for host pinging
$ping = new Net_Ping;
$ping->host = $host;
$ping->port = $host['ping_port'];
// perform the appropriate ping check of the host, 100ms TimeOut, and just 2 retries
$ping_results = $ping->ping(AVAIL_PING, $host['ping_method'], 100, 1);
if ($ping_results === true) {
extdb_log('extenddb_utilities_action dont purge wifi device: '. $host['id']. ' description: '. $host['description'] );
} else {
extdb_log('extenddb_utilities_action purge wifi device: '. $host['id']. ' description: '. $host['description'].' reps: '.$ping->ping_response );
api_device_remove( $host['id'] );
}
}
extdb_log('extenddb_utilities_action end purge: ' );
}
top_header();
utilities();
bottom_footer();
} elseif ($action=='extenddb_purge' ) {
// purge the disabled device that are not pingable
// get device list
$dbquery = "SELECT * FROM host WHERE disabled='on'
AND availability > 0
AND isWifi != 'on'
ORDER BY id";
$extenddb_purge = db_fetch_assoc($dbquery);
extdb_log('extenddb_utilities_action nb device: '. count($extenddb_purge) );
if( $extenddb_purge > 0 ) {
foreach ($extenddb_purge as $host) {
// create new ping socket for host pinging
$ping = new Net_Ping;
$ping->host = $host;
$ping->port = $host['ping_port'];
// perform the appropriate ping check of the host, 10ms TimeOut, and just 2 retries
$ping_results = $ping->ping(AVAIL_PING, $host['ping_method'], 10, 1);
if ($ping_results === true) {
extdb_log('extenddb_utilities_action dont purge device: '. $host['id']. ' description: '. $host['description'] );
} else {
extdb_log('extenddb_utilities_action purge device: '. $host['id']. ' description: '. $host['description'].' reps: '.$ping->ping_response );
api_device_remove( $host['id'] );
}
}
extdb_log('extenddb_utilities_action end purge: ' );
}
top_header();
utilities();
bottom_footer();
} else if ($action == 'extenddb_export_model_SN') {
data_export();
} elseif ($action == 'extenddb_count') { //**************Display the list of model and number of each one of it
top_header();
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'filter' => array(
'filter' => FILTER_DEFAULT,
'pageset' => true,
'default' => ''
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'occurence',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_extenddbcount');
/* ================= input validation ================= */
if (get_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
$refresh['seconds'] = '300';
$refresh['page'] = 'utilities.php?action=extenddb_count&header=false';
$refresh['logout'] = 'false';
set_page_refresh($refresh);
?>
<script type="text/javascript">
function applyFilter() {
strURL = 'utilities.php?action=extenddb_count';
strURL += '&rows=' + $('#rows').val();
strURL += '&filter=' + $('#filter').val();
strURL += '&header=false';
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = urlPath+'utilities.php?action=extenddb_count&clear=1&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#count_model').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
<?php
html_start_box(__('Extenddb Device model'), '100%', '', '3', 'center', '');
?>
<tr class='even noprint'>
<td>
<form id='count_model' action='utilities.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search');?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='filter' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
<td>
<?php print __('Rows');?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default');?></option>
<?php
if (cacti_sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . html_escape($value) . "</option>\n";
}
}
?>
</select>
</td>
<td>
<span>
<input type='submit' class='ui-button ui-corner-all ui-widget' id='refresh' value='<?php print __esc_x('Button: use filter settings', 'Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='clear' value='<?php print __esc_x('Button: reset filter settings', 'Clear');?>' title='<?php print __esc('Clear Filters');?>'>
</span>
</td>
</tr>
</table>
<input type='hidden' name='action' value='extenddb_count'>
</form>
</td>
</tr>
<?php
html_end_box();
$sql_where = " WHERE true ";
/* filter by search string */
if (get_request_var('filter') != '') {
$sql_where .= ' AND pehm.model LIKE ' . db_qstr('%' . get_request_var('filter') . '%');
}
// retrieve all model count uniquness
$total_rows = db_fetch_cell("SELECT count(distinct(pehm.model)) FROM plugin_extenddb_host_model AS pehm". $sql_where);
extdb_log('total_rows :' .print_r($total_rows, true) );
// count each of it
$extenddb_count = db_fetch_assoc("SELECT count(pehm.model) AS occurence, pehm.model AS model FROM plugin_extenddb_host_model AS pehm ". $sql_where
." GROUP BY model");
extdb_log('extenddb_count :' .print_r($extenddb_count, true) );
// sort it
extdb_log('model_count order before:' .print_r($extenddb_count, true) );
$model = array_column( $extenddb_count, 'model' );
$occurence = array_column( $extenddb_count, 'occurence' );
if( get_request_var('sort_column') == 'occurence') {
if( get_request_var('sort_direction') == 'ASC' )
array_multisort( $occurence, SORT_ASC, $extenddb_count );
else
array_multisort( $occurence, SORT_DESC, $extenddb_count );
} else {
if( get_request_var('sort_direction') == 'ASC' )
array_multisort( $model, SORT_ASC, $extenddb_count );
else
array_multisort( $model, SORT_DESC, $extenddb_count );
}
extdb_log('model_count order after: ' .print_r($extenddb_count, true) );
// define the limit, based on the size of the display
$extenddb_count = array_slice($extenddb_count, $rows*(get_request_var('page')-1), $rows, $preserve_keys = true);
/* generate page list */
$nav = html_nav_bar('utilities.php?action=extenddb_count&filter=' . get_request_var('filter'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 11, __('Entries'), 'page', 'main');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(
'model' => array(__('Device model'), 'ASC'),
'occurence' => array(__('Number of Occurence'), 'ASC'));
html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), 1, 'utilities.php?action=extenddb_count');
if (cacti_sizeof($extenddb_count)) {
foreach ($extenddb_count as $item) {
if( empty($item['model']) ) $item['model'] = 'empty';
form_alternate_row('line' . $item['model'], false);
form_selectable_cell(filter_value($item['model'], get_request_var('filter'), 'utilities.php?action=extenddb_display&sort_column=description&model=' . $item['model']), $item['model'].'&page=1');
form_selectable_cell(filter_value($item['occurence'], get_request_var('filter')), $item['occurence']);
form_end_row();
}
}
html_end_box();
if (cacti_sizeof($extenddb_count)) {
print $nav;
}
?>
<script type='text/javascript'>
$('.tooltip').tooltip({
track: true,
show: 250,
hide: 250,
position: { collision: "flipfit" },
content: function() { return $(this).attr('title'); }
});
</script>
<?php
} elseif ($action == 'extenddb_display') {
top_header();
// Show list of a specific model
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'model' => array(
'filter' => FILTER_DEFAULT,
),
'filter' => array(
'filter' => FILTER_DEFAULT,
'pageset' => true,
'default' => ''
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'hostname',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_extenddbdisp');
/* ================= input validation ================= */
if (get_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
if( get_request_var('model') == 'empty' ) set_request_var('model', '');
$model = get_request_var('model');
$refresh['seconds'] = '300';
$refresh['page'] = 'utilities.php?action=extenddb_display&header=false&model='.$model;
$refresh['logout'] = 'false';
set_page_refresh($refresh);
?>
<script type="text/javascript">
function applyFilter() {
strURL = 'utilities.php?action=extenddb_display';
strURL += '&model=' +$('#model').val();
strURL += '&rows=' + $('#rows').val();
strURL += '&filter=' + $('#filter').val();
strURL += '&header=false';
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = urlPath+'utilities.php?action=extenddb_count&clear=1&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#model').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
<?php
html_start_box(__('Extenddb Device model'), '100%', '', '3', 'center', '');
?>
<tr class='even noprint'>
<id='model' value=<?php print (get_request_var('model'))?> >
<td>
<form id='model' action='utilities.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search');?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='filter' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
<td>
<?php print __('Rows');?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default');?></option>
<?php
if (cacti_sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . html_escape($value) . "</option>\n";
}
}
?>
</select>
</td>
<td>
<span>
<input type='submit' class='ui-button ui-corner-all ui-widget' id='refresh' value='<?php print __esc_x('Button: use filter settings', 'Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='clear' value='<?php print __esc_x('Button: reset filter settings', 'Clear');?>' title='<?php print __esc('Clear Filters');?>'>
</span>
</td>
</tr>
</table>
<input type='hidden' name='action' value='extenddb_display'>
</form>
</td>
</tr>
<?php
html_end_box();
$sql_where = "";
/* filter by search string */
if( get_request_var('model') == 'empty' || get_request_var('model') == '' ) {
$sql_where .= ' LEFT JOIN plugin_extenddb_host_model AS pehm ON pehm.host_id=host.id
WHERE ( pehm.host_id IS NULL) ';
} else {
$sql_where .= " INNER JOIN plugin_extenddb_host_model as pehm ON pehm.host_id=host.id
INNER JOIN plugin_extenddb_host_serial_no as pehs ON pehs.host_id=host.id
WHERE pehm.model LIKE ". db_qstr('%' . get_request_var('model') . '%');
}
$sql_where .= " AND host.disabled != 'on' ";
$extenddb_display_sqlquery = "SELECT COUNT(DISTINCT(pehs.serial_no)) FROM host ".$sql_where;
$total_rows = db_fetch_cell($extenddb_display_sqlquery);
extdb_log('type count query: '.$extenddb_display_sqlquery);
// group by serial number to have only one entry of each
$sql_where .= " GROUP BY pehs.serial_no ";
$extenddb_display_sql = "SELECT host.id as id, host.hostname as hostname, host.description as description, pehs.serial_no as serial_no FROM host
$sql_where
ORDER BY " . get_request_var('sort_column') . ' ' . get_request_var('sort_direction') . '
LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;
$extenddb_display = db_fetch_assoc($extenddb_display_sql);
extdb_log('type display query: '.$extenddb_display_sql);
//SELECT id, hostname, description, serial_no FROM host WHERE model LIKE 'C9200L-24P-4X' ORDER BY description ASC LIMIT 50,50
/* generate page list */
$nav = html_nav_bar('utilities.php?action=extenddb_display&filter=' . get_request_var('filter').'&model='.get_request_var('model'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 11, __('Entries'), 'page', 'main');
print $nav;
$display_text = array(
'hostname' => array(__('Device Hostname'), 'ASC'),
'description' => array(__('Device Description'), 'ASC'),
'serial_no' => array(__('Device SerialNumber'), ''));
html_start_box('', '100%', '', '3', 'center', '');
html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), 1, 'utilities.php?action=extenddb_display');
if (cacti_sizeof($extenddb_display)) {
foreach ($extenddb_display as $item) {
form_alternate_row('line' . $item['hostname'], false);
form_selectable_cell(filter_value($item['hostname'], get_request_var('filter'), 'host.php?action=edit&id=' . $item['id']), $item['id']);
form_selectable_cell(filter_value($item['description'], get_request_var('filter'), 'host.php?action=edit&id=' . $item['id']), $item['id']);
form_selectable_cell(filter_value($item['serial_no'], get_request_var('filter')), $item['serial_no']);
form_end_row();
}
}
html_end_box();
if (cacti_sizeof($extenddb_display)) {
print $nav;
}
?>
<script type='text/javascript'>
$('.tooltip').tooltip({
track: true,
show: 250,
hide: 250,
position: { collision: "flipfit" },
content: function() { return $(this).attr('title'); }
});
</script>
<?php
}
cacti_log( 'Extenddb utilities end: '. $action, false, "EXTENDDB" ); // Log somme information on cactilog
return $action;
}
function extenddb_api_device_new($hostrecord_array) {
$snmpsysobjid = ".1.3.6.1.2.1.1.2.0"; // ObjectID
$snmpsysdescr = ".1.3.6.1.2.1.1.1.0"; // system description
extdb_log('extenddb_api_device_new: '.print_r($hostrecord_array['description'], true) );
// check valid call
if( !array_key_exists('disabled', $hostrecord_array ) ) {
extdb_log('extenddb_api_device_new Not valid call: '. $hostrecord_array['description'] .' '. $hostrecord_array['hostname']);
return $hostrecord_array;
}
// get valid host from DB
$host = db_fetch_row("SELECT * FROM host WHERE hostname='".$hostrecord_array['hostname']."' OR description='".$hostrecord_array['description']."'");
if( empty($host) ){
extdb_log('extenddb_api_device_new Unknown hostname in Extenddb:'. $hostrecord_array['description'] .' '. $hostrecord_array['hostname'] );
return $hostrecord_array;
}
// don't do it for Phone and Wifi
if ($host['isPhone'] == 'On' ) {
extdb_log('extenddb_api_device_new Exit Extenddb skip for phone');
return $hostrecord_array;
}
// don't do it for disabled and no snmp
if ($host['disabled'] == 'on' || $host['snmp_version'] == 0 ) {
extdb_log('extenddb_api_device_new Exit Extenddb Disabled or no snmp');
return $hostrecord_array;
}
if( empty($host['snmp_sysObjectID']) ) {
// parse device to find snmp_sysObjectID: OID: .1.3.6.1.4.1.9.1.2134
// on DB iso.3.6.1.4.1.9.1.2134'
$host_data = cacti_snmp_get( $host['hostname'], $host['snmp_community'], $snmpsysobjid,
$host['snmp_version'], $host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'] );
$regex = '~.[0-9].*\.([0-9].*)~';
preg_match( $regex, $host_data, $result ); // extract the OID of the switch number from the snmp query
extdb_log('extenddb_api_device_new host_data: '. $hostrecord_array['description'] .' '. $hostrecord_array['hostname'].' '.$host['snmp_sysObjectID']);
$host['snmp_sysObjectID'] = 'iso.3.6.1.4.1.9.1.'.$result[1];
$hostrecord_array['snmp_sysObjectID'] = $host['snmp_sysObjectID'];
extdb_log('host_data: '.$host['snmp_sysObjectID']);
$host['snmp_sysDescr'] = cacti_snmp_get( $host['hostname'], $host['snmp_community'], $snmpsysdescr,
$host['snmp_version'], $host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'] );
$hostrecord_array['snmp_sysDescr'] = $host['snmp_sysDescr'];
extdb_log('host_id: '.$host['snmp_sysDescr']);
}
// do it for Cisco model
if( mb_stripos( $host['snmp_sysDescr'], 'cisco') === false ) {
extdb_log('extenddb_api_device_new Exit Extenddb not cisco' );
return $hostrecord_array;
}
/*
if (isset_request_var('isPhone')) {
$hostrecord_array['isPhone'] = form_input_validate(get_nfilter_request_var('isPhone'), 'isPhone', '', true, 3);
} else {
$hostrecord_array['isPhone'] = form_input_validate('off', 'isPhone', '', true, 3);
}
*/
//****** check the serial
$serial_nos = get_SN( $host, $host['snmp_sysObjectID'] );
if( empty($serial_nos) ) {
extdb_log('extenddb_api_device_new can t SNMP read SN on ' . $host['description'] );
return $hostrecord_array;
}
if ( array_key_exists('id', $hostrecord_array)) {
foreach(explode( '|', $serial_nos) as $key => $serial_no ) {
$mysql_insert = "INSERT INTO plugin_extenddb_host_serial_no (id, host_id, serial_no) VALUES('".$key."', '".$hostrecord_array['id']."', '".$serial_no."')
ON DUPLICATE KEY UPDATE serial_no='".$serial_no."'";
db_execute($mysql_insert);
extdb_log('extenddb_api_device_new End Extenddb serial_no: '.print_r($mysql_insert, true) );
}
} else {
cacti_log( 'Cant find Serial for device: '.$host['description'], false, "EXTENDDB" );
}
//****** check the model
$models = get_model( $host );
if( empty($models) ) {
extdb_log('extenddb_api_device_new can t SNMP read model on ' . $host['description'] );
return $hostrecord_array;
}
if (array_key_exists('id', $hostrecord_array)) {
foreach(explode( '|', $models) as $key => $model ) {
$mysql_insert = "INSERT INTO plugin_extenddb_host_model (id, host_id, model) VALUES('".$key."', '".$hostrecord_array['id']."', '".$model."')
ON DUPLICATE KEY UPDATE model='".$model."'";
db_execute($mysql_insert);
extdb_log('extenddb_api_device_new End Extenddb model: '.print_r($mysql_insert, true) );
}
} else {
cacti_log( 'Cant find Model for device: '.$host['description'], false, "EXTENDDB" );
}
return $hostrecord_array;
}
function extenddb_config_settings () {
global $tabs, $settings;