/
DB.pm
1755 lines (1441 loc) · 51.6 KB
/
DB.pm
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
package PandoraFMS::DB;
##########################################################################
# Database Package
# Pandora FMS. the Flexible Monitoring System. http://www.pandorafms.org
##########################################################################
# Copyright (c) 2005-2021 Artica Soluciones Tecnologicas S.L
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; version 2
# 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.
##########################################################################
use strict;
use warnings;
use DBI;
use Carp qw/croak/;
BEGIN { push @INC, '/usr/lib/perl5'; }
use PandoraFMS::Tools;
#use Data::Dumper;
require Exporter;
our @ISA = ("Exporter");
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
add_address
add_new_address_agent
db_concat
db_connect
db_history_connect
db_delete_limit
db_disconnect
db_do
db_get_lock
db_get_pandora_lock
db_insert
db_insert_get_values
db_insert_from_array_hash
db_insert_from_hash
db_process_insert
db_process_update
db_release_lock
db_release_pandora_lock
db_string
db_text
db_update
db_update_hash
db_update_get_values
set_update_agent
set_update_agentmodule
get_action_id
get_addr_id
get_agent_addr_id
get_agent_id
get_agent_ids_from_alias
get_agent_address
get_agent_alias
get_agent_group
get_agent_name
get_agent_module_id
get_alert_template_module_id
get_alert_template_name
get_command_id
get_console_api_url
get_db_rows
get_db_rows_limit
get_db_single_row
get_db_value
get_db_value_limit
get_first_server_name
get_group_id
get_group_name
get_module_agent_id
get_module_group_id
get_module_group_name
get_module_id
get_module_name
get_nc_profile_name
get_pen_templates
get_nc_profile_advanced
get_os_id
get_os_name
get_plugin_id
get_profile_id
get_priority_name
get_server_id
get_tag_id
get_tag_name
get_group_name
get_template_id
get_template_module_id
get_user_disabled
get_user_exists
get_user_profile_id
get_group_children
get_agentmodule_custom_id
set_agentmodule_custom_id
is_agent_address
is_group_disabled
get_agent_status
get_agent_modules
get_agentmodule_status
get_agentmodule_status_str
get_agentmodule_data
set_ssl_opts
db_synch_insert
db_synch_update
db_synch_delete
db_synch
$RDBMS
$RDBMS_QUOTE
$RDBMS_QUOTE_STRING
);
# Relational database management system in use
our $RDBMS = '';
# For fields, character used to quote reserved words in the current RDBMS
our $RDBMS_QUOTE = '';
# For strings, Character used to quote in the current RDBMS
our $RDBMS_QUOTE_STRING = '';
# SSL options.
my $SSL_OPTS = '';
##########################################################################
## Connect to the DB.
##########################################################################
sub db_connect ($$$$$$) {
my ($rdbms, $db_name, $db_host, $db_port, $db_user, $db_pass) = @_;
if ($rdbms eq 'mysql') {
$RDBMS = 'mysql';
$RDBMS_QUOTE = '`';
$RDBMS_QUOTE_STRING = '"';
# Connect to MySQL
my $dbh = DBI->connect("DBI:mysql:$db_name:$db_host:$db_port;$SSL_OPTS", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
return undef unless defined ($dbh);
# Enable auto reconnect
$dbh->{'mysql_auto_reconnect'} = 1;
# Enable character semantics
$dbh->{'mysql_enable_utf8'} = 1;
return $dbh;
}
elsif ($rdbms eq 'postgresql') {
$RDBMS = 'postgresql';
$RDBMS_QUOTE = '"';
$RDBMS_QUOTE_STRING = "'";
# Connect to PostgreSQL
my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
return undef unless defined ($dbh);
return $dbh;
}
elsif ($rdbms eq 'oracle') {
$RDBMS = 'oracle';
$RDBMS_QUOTE = '"';
$RDBMS_QUOTE_STRING = '\'';
# Connect to Oracle
my $dbh = DBI->connect("DBI:Oracle:dbname=$db_name;host=$db_host;port=$db_port;sid=$db_name", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
return undef unless defined ($dbh);
# Set date format
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$dbh->do("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$dbh->do("ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'");
# Configuration to avoid errors when working with CLOB columns
$dbh->{'LongReadLen'} = 66000;
$dbh->{'LongTruncOk'} = 1;
return $dbh;
}
return undef;
}
##########################################################################
## Connect to a history DB associated to given dbh.
##########################################################################
sub db_history_connect {
my ($dbh, $pa_config) = @_;
my %conf;
$conf{'history_db_enabled'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", "history_db_enabled");
$conf{'history_db_host'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", "history_db_host");
$conf{'history_db_port'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", "history_db_port");
$conf{'history_db_name'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", "history_db_name");
$conf{'history_db_user'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", "history_db_user");
$conf{'history_db_pass'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", "history_db_pass");
my $history_dbh = ($conf{'history_db_enabled'} eq '1') ? db_connect ($pa_config->{'dbengine'}, $conf{'history_db_name'},
$conf{'history_db_host'}, $conf{'history_db_port'}, $conf{'history_db_user'}, $conf{'history_db_pass'}) : undef;
return $history_dbh;
}
########################################################################
## Disconnect from the DB.
########################################################################
sub db_disconnect ($) {
my $dbh = shift;
$dbh->disconnect();
}
########################################################################
## Return local console API url.
########################################################################
sub get_console_api_url ($$) {
my ($pa_config, $dbh) = @_;
# Only if console_api_url was not defined
if( !defined($pa_config->{"console_api_url"}) ) {
my $console_api_url = PandoraFMS::Config::pandora_get_tconfig_token(
$dbh, 'public_url', ''
);
my $include_api = 'include/api.php';
# If public_url is empty in database
if ( $console_api_url eq '' ) {
$pa_config->{"console_api_url"} = 'http://127.0.0.1/pandora_console/' . $include_api;
logger($pa_config, "Assuming default path for API url: " . $pa_config->{"console_api_url"}, 3);
} else {
if ($console_api_url !~ /\/$/) {
$console_api_url .= '/';
}
$pa_config->{"console_api_url"} = $console_api_url . $include_api;
}
}
return $pa_config->{'console_api_url'};
}
########################################################################
## Return action ID given the action name.
########################################################################
sub get_action_id ($$) {
my ($dbh, $action_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id FROM talert_actions WHERE name = ?", $action_name);
return defined ($rc) ? $rc : -1;
}
########################################################################
## Return command ID given the command name.
########################################################################
sub get_command_id ($$) {
my ($dbh, $command_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id FROM talert_commands WHERE name = ?", safe_input($command_name));
return defined ($rc) ? $rc : -1;
}
########################################################################
## Return agent ID given the agent name.
########################################################################
sub get_agent_id ($$) {
my ($dbh, $agent_name) = @_;
my $is_meta = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token like 'metaconsole'");
my $rc;
if($is_meta == 1) {
$rc = get_db_value ($dbh, "SELECT id_agente FROM tmetaconsole_agent WHERE nombre = ?", safe_input($agent_name));
} else {
$rc = get_db_value ($dbh, "SELECT id_agente FROM tagente WHERE nombre = ?", safe_input($agent_name));
}
return defined ($rc) ? $rc : -1;
}
########################################################################
## Return agent IDs given an agent alias.
########################################################################
sub get_agent_ids_from_alias ($$) {
my ($dbh, $agent_alias) = @_;
my @rc = get_db_rows ($dbh, "SELECT id_agente, nombre FROM tagente WHERE alias = ?", safe_input($agent_alias));
return @rc;
}
########################################################################
## Return server ID given the name of server.
########################################################################
sub get_server_id ($$$) {
my ($dbh, $server_name, $server_type) = @_;
my $rc = get_db_value ($dbh, "SELECT id_server FROM tserver
WHERE BINARY name = ? AND server_type = ?",
$server_name, $server_type);
return defined ($rc) ? $rc : -1;
}
########################################################################
## Return the ID of a tag given the tag name.
########################################################################
sub get_tag_id ($$) {
my ($dbh, $tag_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id_tag FROM ttag
WHERE name = ?",
safe_input($tag_name));
return defined ($rc) ? $rc : -1;
}
########################################################################
## Return the name of a tag given its id.
########################################################################
sub get_tag_name ($$) {
my ($dbh, $id) = @_;
my $rc = get_db_value(
$dbh, "SELECT name FROM ttag
WHERE id_tag = ?",
safe_input($id)
);
return $rc;
}
########################################################################
## Return the first enabled server name found.
########################################################################
sub get_first_server_name ($) {
my ($dbh) = @_;
my $rc = get_db_value ($dbh, "SELECT name FROM tserver");
return defined ($rc) ? $rc : "";
}
########################################################################
## Return group ID given the group name.
########################################################################
sub get_group_id ($$) {
my ($dbh, $group_name) = @_;
my $rc = get_db_value ($dbh, 'SELECT id_grupo FROM tgrupo WHERE ' . db_text ('nombre') . ' = ?', safe_input($group_name));
return defined ($rc) ? $rc : -1;
}
########################################################################
# Return a array of groups, children of given parent.
########################################################################
sub get_group_children ($$$;$);
sub get_group_children ($$$;$) {
my ($dbh, $parent, $ignorePropagate, $href_groups) = @_;
if (is_empty($href_groups)) {
my @groups = get_db_rows($dbh, 'SELECT * FROM tgrupo');
my %groups = map {
$_->{'id_grupo'} => $_
} @groups;
$href_groups = \%groups;
}
my $return = {};
foreach my $id_grupo (keys %{$href_groups}) {
if ($id_grupo eq 0) {
next;
}
my $g = $href_groups->{$id_grupo};
if ($ignorePropagate || $parent eq 0 || $href_groups->{$parent}{'propagate'}) {
if ($g->{'parent'} eq $parent) {
$return->{$g->{'id_grupo'}} = $g;
if ($g->{'propagate'} || $ignorePropagate) {
$return = add_hashes(
$return,
get_group_children($dbh, $g->{'id_grupo'}, $ignorePropagate, $href_groups)
);
}
}
}
}
return $return;
}
########################################################################
## Return OS ID given the OS name.
########################################################################
sub get_os_id ($$) {
my ($dbh, $os_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id_os FROM tconfig_os WHERE name = ?", $os_name);
return defined ($rc) ? $rc : -1;
}
########################################################################
## Return OS name given the OS id.
########################################################################
sub get_os_name ($$) {
my ($dbh, $os_id) = @_;
my $rc = get_db_value ($dbh, "SELECT name FROM tconfig_os WHERE id_os = ?", $os_id);
return defined ($rc) ? $rc : -1;
}
##########################################################################
## SUB get_agent_name (agent_id)
## Return agent group id, given "agent_id"
##########################################################################
sub get_agent_group ($$) {
my ($dbh, $agent_id) = @_;
my $group_id = get_db_value ($dbh, "SELECT id_grupo
FROM tagente
WHERE id_agente = ?", $agent_id);
return 0 unless defined ($group_id);
return $group_id;
}
########################################################################
## SUB get_agent_name (agent_id)
## Return agent name, given "agent_id"
########################################################################
sub get_agent_name ($$) {
my ($dbh, $agent_id) = @_;
return get_db_value ($dbh, "SELECT nombre
FROM tagente
WHERE id_agente = ?", $agent_id);
}
########################################################################
## SUB get_agent_alias (agent_id)
## Return agent alias, given "agent_id"
########################################################################
sub get_agent_alias ($$) {
my ($dbh, $agent_id) = @_;
return get_db_value ($dbh, "SELECT alias
FROM tagente
WHERE id_agente = ?", $agent_id);
}
########################################################################
## SUB agents_get_modules (agent_id, fields, filters)
## Return the list of modules, given "agent_id"
########################################################################
sub get_agent_modules ($$$$$) {
my ($pa_config, $dbh, $agent_id, $fields, $filters) = @_;
my $str_filter = '';
foreach my $key (keys %$filters) {
$str_filter .= ' AND ' . $key . " = " . $filters->{$key};
}
my @rows = get_db_rows($dbh, "SELECT *
FROM tagente_modulo
WHERE id_agente = ?" . $str_filter, $agent_id);
return @rows;
}
########################################################################
## SUB get_agentmodule_data (id_agent_module, period, date)
## Return The data for module in a period of time.
########################################################################
sub get_agentmodule_data ($$$$$) {
my ($pa_config, $dbh, $id_agent_module, $period, $date) = @_;
if ($date < 1) {
# Get current timestamp
$date = time ();
}
my $datelimit = $date - $period;
my @rows = get_db_rows($dbh,
"SELECT datos AS data, utimestamp
FROM tagente_datos
WHERE id_agente_modulo = ?
AND utimestamp > ? AND utimestamp <= ?
ORDER BY utimestamp ASC",
$id_agent_module, $datelimit, $date);
#logger($pa_config, "SELECT datos AS data, utimestamp
# FROM tagente_datos
# WHERE id_agente_modulo = " . $id_agent_module . "
# AND utimestamp > " . $datelimit . " AND utimestamp <= " . $date . "
# ORDER BY utimestamp ASC", 1);
return @rows;
}
##########################################################################
## Return module custom ID given the module id.
##########################################################################
sub get_agentmodule_custom_id ($$) {
my ($dbh, $id_agent_module) = @_;
my $rc = get_db_value(
$dbh,
"SELECT custom_id FROM tagente_modulo WHERE id_agente_modulo = ?",
safe_input($id_agent_module)
);
return defined($rc) ? $rc : undef;
}
##########################################################################
## Updates module custom ID given the module id and custom Id.
##########################################################################
sub set_agentmodule_custom_id ($$$) {
my ($dbh, $id_agent_module, $custom_id) = @_;
my $rc = db_update(
$dbh,
"UPDATE tagente_modulo SET custom_id = ? WHERE id_agente_modulo = ?",
safe_input($custom_id),
safe_input($id_agent_module)
);
return defined($rc) ? ($rc eq '0E0' ? 0 : $rc) : -1;
}
########################################################################
## SUB get_agentmodule_status (agent_module_id)
## Return agent module status. given "agent_module_id"
########################################################################
sub get_agentmodule_status($$$) {
my ($pa_config, $dbh, $agent_module_id) = @_;
my $status = get_db_value($dbh, 'SELECT estado
FROM tagente_estado
WHERE id_agente_modulo = ?', $agent_module_id);
return $status;
}
########################################################################
## Return the status of an agent module as a string.
########################################################################
sub get_agentmodule_status_str($$$) {
my ($pa_config, $dbh, $agent_module_id) = @_;
my $status = get_db_value($dbh, 'SELECT estado
FROM tagente_estado
WHERE id_agente_modulo = ?', $agent_module_id);
return 'N/A' unless defined($status);
return 'Normal' if ($status == 0);
return 'Critical' if ($status == 1);
return 'Warning' if ($status == 2);
return 'Unknown' if ($status == 3);
return 'Not init' if ($status == 4);
return 'N/A';
}
########################################################################
## SUB get_get_status (agent_id)
## Return agent status, given "agent_id"
########################################################################
sub get_agent_status ($$$) {
my ($pa_config, $dbh, $agent_id) = @_;
my @modules = get_agent_modules ($pa_config, $dbh,
$agent_id, 'id_agente_modulo', {'disabled' => 0});
#logger($pa_config, Dumper(@modules), 5);
# The status are:
# 3 -> AGENT_MODULE_STATUS_UNKNOW
# 4 -> AGENT_MODULE_STATUS_CRITICAL_ALERT
# 1 -> AGENT_MODULE_STATUS_CRITICAL_BAD
# 2 -> AGENT_MODULE_STATUS_WARNING
# 0 -> AGENT_MODULE_STATUS_NORMAL
my $module_status = 3;
my $modules_async = 0;
foreach my $module (@modules) {
my $m_status = get_agentmodule_status($pa_config, $dbh,
$module->{'id_agente_modulo'});
#This is the order to check
# AGENT_MODULE_STATUS_CRITICAL_ALERT
# AGENT_MODULE_STATUS_CRITICAL_BAD
# AGENT_MODULE_STATUS_WARNING
# AGENT_MODULE_STATUS_UNKNOWN
# AGENT_MODULE_STATUS_NORMAL
if ($m_status == 4) {
$module_status = 4;
}
elsif ($module_status != 4) {
if ($m_status == 1) {
$module_status = 1;
}
elsif ($module_status != 1) {
if ($m_status == 2) {
$module_status = 2;
}
elsif ($module_status != 2) {
if ($m_status == 0) {
$module_status = 0;
}
}
}
}
my $module_type = get_db_value($dbh, 'SELECT id_tipo_modulo
FROM tagente_modulo
WHERE id_agente_modulo = ?', $module->{'id_agente_modulo'});
if (($module_type >= 21 && $module_type <= 23) ||
$module_type == 100) {
$modules_async++;
}
}
my $count_modules = scalar(@modules);
# If all the modules are asynchronous or keep alive, the group cannot be unknown
if ($modules_async < $count_modules) {
my $last_contact = get_db_value($dbh,
'SELECT (UNIX_TIMESTAMP(ultimo_contacto) + (intervalo * 2)) AS last_contact
FROM tagente WHERE id_agente = ?', $agent_id);
if ($last_contact < time ()) {
return 3;
}
}
return $module_status;
}
########################################################################
## SUB get_module_agent_id (agent_module_id)
## Return agent id, given "agent_module_id"
########################################################################
sub get_module_agent_id ($$) {
my ($dbh, $agent_module_id) = @_;
return get_db_value ($dbh, "SELECT id_agente FROM tagente_modulo WHERE id_agente_modulo = ?", $agent_module_id);
}
########################################################################
## SUB get_agent_address (id_agente)
## Return agent address, given "agent_id"
########################################################################
sub get_agent_address ($$) {
my ($dbh, $agent_id) = @_;
return get_db_value ($dbh, "SELECT direccion FROM tagente WHERE id_agente = ?", $agent_id);
}
########################################################################
## SUB get_module_name(module_id)
## Return the module name, given "module_id"
########################################################################
sub get_module_name ($$) {
my ($dbh, $module_id) = @_;
return get_db_value ($dbh, "SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = ?", $module_id);
}
########################################################################
## Return module id given the module name and agent id.
########################################################################
sub get_agent_module_id ($$$) {
my ($dbh, $module_name, $agent_id) = @_;
my $rc = get_db_value ($dbh, "SELECT id_agente_modulo FROM tagente_modulo WHERE delete_pending = 0 AND nombre = ? AND id_agente = ?", safe_input($module_name), $agent_id);
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return template id given the template name.
##########################################################################
sub get_template_id ($$) {
my ($dbh, $template_name) = @_;
my $field;
if ($RDBMS eq 'oracle') {
$field = "to_char(name)";
}
else {
$field = "name";
}
my $rc = get_db_value ($dbh, "SELECT id FROM talert_templates WHERE $field = ?", safe_input($template_name));
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return the module template id given the module id and the template id.
##########################################################################
sub get_template_module_id ($$$) {
my ($dbh, $module_id, $template_id) = @_;
my $rc = get_db_value ($dbh, "SELECT id FROM talert_template_modules WHERE id_agent_module = ? AND id_alert_template = ?", $module_id, $template_id);
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Returns true if the given group is disabled, false otherwise.
##########################################################################
sub is_group_disabled ($$) {
my ($dbh, $group_id) = @_;
return get_db_value ($dbh, "SELECT disabled FROM tgrupo WHERE id_grupo = ?", $group_id);
}
##########################################################################
## Return module ID given the module name.
##########################################################################
sub get_module_id ($$) {
my ($dbh, $module_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id_tipo FROM ttipo_modulo WHERE nombre = ?", safe_input($module_name));
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return disabled bit frin a user.
##########################################################################
sub get_user_disabled ($$) {
my ($dbh, $user_id) = @_;
my $rc = get_db_value ($dbh, "SELECT disabled FROM tusuario WHERE id_user = ?", safe_input($user_id));
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return 1 if user exists or -1 if not
##########################################################################
sub get_user_exists ($$) {
my ($dbh, $user_id) = @_;
my $rc = get_db_value ($dbh, "SELECT id_user FROM tusuario WHERE id_user = ?", safe_input($user_id));
return defined ($rc) ? 1 : -1;
}
##########################################################################
## Return plugin ID given the plugin name.
##########################################################################
sub get_plugin_id ($$) {
my ($dbh, $plugin_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id FROM tplugin WHERE name = ?", safe_input($plugin_name));
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return module group ID given the module group name.
##########################################################################
sub get_module_group_id ($$) {
my ($dbh, $module_group_name) = @_;
if (!defined($module_group_name) || $module_group_name eq '') {
return 0;
}
my $rc = get_db_value ($dbh, "SELECT id_mg FROM tmodule_group WHERE name = ?", safe_input($module_group_name));
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return module group name given the module group id.
##########################################################################
sub get_module_group_name ($$) {
my ($dbh, $module_group_id) = @_;
return get_db_value ($dbh, "SELECT name FROM tmodule_group WHERE id_mg = ?", $module_group_id);
}
##########################################################################
## Return a network component's profile name given its ID.
##########################################################################
sub get_nc_profile_name ($$) {
my ($dbh, $nc_id) = @_;
return get_db_value ($dbh, "SELECT * FROM tnetwork_profile WHERE id_np = ?", $nc_id);
}
##########################################################################
## Return all network component's profile ids matching given PEN.
##########################################################################
sub get_pen_templates($$) {
my ($dbh, $pen) = @_;
my @results = get_db_rows(
$dbh,
'SELECT t.`id_np`
FROM `tnetwork_profile` t
INNER JOIN `tnetwork_profile_pen` pp ON pp.`id_np` = t.`id_np`
INNER JOIN `tpen` p ON pp.pen = p.pen
WHERE p.`pen` = ?',
$pen
);
@results = map {
if (ref($_) eq 'HASH') { $_->{'id_np'} }
else {}
} @results;
return @results;
}
##########################################################################
## Return a network component's profile data and pen list, given its ID.
##########################################################################
sub get_nc_profile_advanced($$) {
my ($dbh, $id_nc) = @_;
return get_db_single_row(
$dbh,
'SELECT t.*,GROUP_CONCAT(p.pen) AS "pen"
FROM `tnetwork_profile` t
LEFT JOIN `tnetwork_profile_pen` pp ON t.id_np = pp.id_np
LEFT JOIN `tpen` p ON pp.pen = p.pen
WHERE t.`id_np` = ?
GROUP BY t.`id_np`',
$id_nc
);
}
##########################################################################
## Return user profile ID given the user id, group id and profile id.
##########################################################################
sub get_user_profile_id ($$$$) {
my ($dbh, $user_id, $profile_id, $group_id) = @_;
my $rc = get_db_value ($dbh, "SELECT id_up FROM tusuario_perfil
WHERE id_usuario = ?
AND id_perfil = ?
AND id_grupo = ?",
safe_input($user_id),
$profile_id,
$group_id);
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return profile ID given the profile name.
##########################################################################
sub get_profile_id ($$) {
my ($dbh, $profile_name) = @_;
my $rc = get_db_value ($dbh, "SELECT id_perfil FROM tperfil WHERE name = ?", safe_input($profile_name));
return defined ($rc) ? $rc : -1;
}
##########################################################################
## Return a group's name given its ID.
##########################################################################
sub get_group_name ($$) {
my ($dbh, $group_id) = @_;
return get_db_value ($dbh, "SELECT nombre FROM tgrupo WHERE id_grupo = ?", $group_id);
}
########################################################################
## Get a single column returned by an SQL query as a hash reference.
########################################################################
sub get_db_value ($$;@) {
my ($dbh, $query, @values) = @_;
# Cache statements
my $sth = $dbh->prepare_cached($query);
$sth->execute(@values);
# Save returned rows
while (my $row = $sth->fetchrow_arrayref()) {
$sth->finish();
return defined ($row->[0]) ? $row->[0] : undef;
}
$sth->finish();
return undef;
}
########################################################################
## Get a single column returned by an SQL query with a LIMIT statement
## as a hash reference.
########################################################################
sub get_db_value_limit ($$$;@) {
my ($dbh, $query, $limit, @values) = @_;
# Cache statements
my $sth;
if ($RDBMS ne 'oracle') {
$sth = $dbh->prepare_cached($query . ' LIMIT ' . int($limit));
} else {
$sth = $dbh->prepare_cached('SELECT * FROM (' . $query . ') WHERE ROWNUM <= ' . int($limit));
}
$sth->execute(@values);
# Save returned rows
while (my $row = $sth->fetchrow_arrayref()) {
$sth->finish();
return defined ($row->[0]) ? $row->[0] : undef;
}
$sth->finish();
return undef;
}
##########################################################################
## Get a single row returned by an SQL query as a hash reference. Returns
## hash or undef on error.
##########################################################################
sub get_db_single_row ($$;@) {
my ($dbh, $query, @values) = @_;
#my @rows;
# Cache statements
my $sth = $dbh->prepare_cached($query);
$sth->execute(@values);
# Save returned rows
while (my $row = $sth->fetchrow_hashref()) {
$sth->finish();
return {map { lc ($_) => $row->{$_} } keys (%{$row})} if ($RDBMS eq 'oracle');
return $row;
}
$sth->finish();
return undef;
}
##########################################################################
## Get all rows returned by an SQL query as a hash reference array.
##########################################################################
sub get_db_rows ($$;@) {
my ($dbh, $query, @values) = @_;
my @rows;
# Cache statements
my $sth = $dbh->prepare_cached($query);
$sth->execute(@values);
# Save returned rows
while (my $row = $sth->fetchrow_hashref()) {
if ($RDBMS eq 'oracle') {
push (@rows, {map { lc ($_) => $row->{$_} } keys (%{$row})});
}
else {
push (@rows, $row);
}
}
$sth->finish();
return @rows;
}
########################################################################
## Get all rows (with a limit clause) returned by an SQL query
## as a hash reference array.
########################################################################
sub get_db_rows_limit ($$$;@) {
my ($dbh, $query, $limit, @values) = @_;
my @rows;
# Cache statements
my $sth;
if ($RDBMS ne 'oracle') {
$sth = $dbh->prepare_cached($query . ' LIMIT ' . $limit);
} else {
$sth = $dbh->prepare_cached('SELECT * FROM (' . $query . ') WHERE ROWNUM <= ' . $limit);
}
$sth->execute(@values);
# Save returned rows
while (my $row = $sth->fetchrow_hashref()) {
if ($RDBMS eq 'oracle') {
push (@rows, {map { lc ($_) => $row->{$_} } keys (%{$row})});
}
else {
push (@rows, $row);