blib/lib/RackMan/Format/Cacti.pm

filter: Statementstmt 23.8% 16 / 67 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 36.3% 4 / 11 Podpod 14.2% 1 / 7 totaltotal 17.0% 21 / 123 CC 17 SCAR 49.0
CC
17
Cov
16%
write
240.0
cacti_configure
6.0
cacti_add_device
6.0
CRAP 134.7
« blib/lib/RackMan/Device/PDU/APC_RackPDU.pm ↑ Summary blib/lib/RackMan/Device/Switch/Cisco_Catalyst.pm »
1 package RackMan::Format::Cacti;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 CONFIG_SECTION => "format:cacti",
10 DEFAULT_PATH => "/var/lib/cacti/cli",
11 DEFAULT_PHP => "/usr/bin/php",
12 2 };
13  
14 2 my @SUDO;
15 2 my @PHP = ( DEFAULT_PHP, "-q" );
16 2 my $CACTI = DEFAULT_PATH;
17  
18  
19 #
20 # write()
21 # -----
22 0 ▶sub write {
Subroutine write: not called CC=15 SCAR=54.8
Pod documented
23 0 my ($class, $args) = @_;
24  
25 0 my $rackdev = $args->{rackdev};
26 0 my $rackman = $args->{rackman};
27 0 my $name = $rackdev->object_name;
28 0 ▶ my $fqdn = $rackdev->attributes->{FQDN} || $name;
Condition0%0/3
$rackdev->attributes->{'FQDN'} || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackdev->attributes->{'FQDN'} || $name
$rackdev->attributes->{'FQDN'} $name
Truth table0%0/3
A || B
A= $rackdev->attributes->{'FQDN'}B= $name
ABresult
1X1
011
000
29  
30 0 cacti_configure($rackman);
31  
32 0 my %is = ( lc($rackdev->object_type) => 1 );
33 0 ▶ $is{cisco_switch} = 1 if $is{switch} and $rackdev->class =~ /Cisco/;
Branch: 1 branch
BranchTrueFalseTotal
if $is{"switch"} and $rackdev->class =~ /Cisco/ 0 0 0
Condition0%0/3
$is{"switch"} and $rackdev->class =~ /Cisco/
!ll&&!rl&&r
000
MC/DC0%0/2
$is{"switch"} and $rackdev->class =~ /Cisco/
$is{"switch"} $rackdev->class =~ /Cisco/
Truth table0%0/3
A and B
A= $is{"switch"}B= $rackdev->class =~ /Cisco/
ABresult
0X0
100
111
34  
35 # determine the template ID from the object type
36 0 ▶ my $tmpl_id = $is{server} ? 3 : $is{cisco_switch} ? 5 : 1;
Branch: 2 branches
BranchTrueFalseTotal
$is{'server'} ? : 0 0 0
$is{'cisco_switch'} ? : 0 0 0
37  
38 # add the device in Cacti
39 0 ▶ print " x adding device $name\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
40 0 ▶ my $host_id = cacti_add_device({
Branch: 1 branch
BranchTrueFalseTotal
my $host_id = cacti_add_device({"description", $name, "ip", $fqdn, "version", 2, "template", $tmpl_id}) or return 0 0 0
41 description => $name, ip => $fqdn,
42 version => 2, template => $tmpl_id,
43 }) or return;
44  
45 0 ▶ if ($tmpl_id > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpl_id > 1) 0 0 0
46 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47 # add data queries
48 0 ▶ print " x adding data queries\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
49  
50 # common options
51 0 my %opts = ( "host-id" => $host_id, "reindex-method" => "fields" );
52  
53 # data query: SNMP - Interface Statistics
54 0 cacti_add_data_query({ %opts, "data-query-id" => 1 });
55  
56 # data query: SNMP - Get Processor Information
57 0 cacti_add_data_query({ %opts, "data-query-id" => 9 });
58  
59 0 ▶ if ($is{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is{"server"}) 0 0 0
60 # data query: ucd/net - Get Monitored Partitions
61 0 cacti_add_data_query({ %opts, "data-query-id" => 2 });
62 }
63  
64 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65 # add graph templates
66 0 ▶ print " x adding graph templates\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
67  
68 # common options
69 0 %opts = ( "host-id" => $host_id );
70  
71 # graph template: Interface - Traffic (bits/sec)
72 0 cacti_add_graph_template({ %opts, "graph-template-id" => 2 });
73  
74 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75 # add non-indexed graphs
76 0 ▶ print " x adding non-indexed graphs\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
77  
78 # common options
79 0 %opts = ( "host-id" => $host_id, "graph-type" => "cg" );
80  
81 0 ▶ if ($is{cisco_switch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is{"cisco_switch"}) 0 0 0
82 # graph: Cisco - CPU Usage
83 0 cacti_add_graphs({ %opts, "graph-template-id" => 18 });
84 }
85  
86 0 ▶ if ($is{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is{"server"}) 0 0 0
87 # graph: ucd/net - Available Disk Space
88 0 cacti_add_graphs({ %opts, "graph-template-id" => 3 });
89  
90 # graph: ucd/net - CPU Usage
91 0 cacti_add_graphs({ %opts, "graph-template-id" => 4 });
92  
93 # graph: ucd/net - Load Average
94 0 cacti_add_graphs({ %opts, "graph-template-id" => 11 });
95  
96 # graph: ucd/net - Memory Usage
97 0 cacti_add_graphs({ %opts, "graph-template-id" => 13 });
98 }
99 }
100 }
101  
102  
103 #
104 # cacti_configure()
105 # ---------------
106 0 ▶sub cacti_configure {
Subroutine cacti_configure: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $rackman = shift;
108  
109 0 $CACTI = $rackman->config->val(CONFIG_SECTION, "path", DEFAULT_PATH);
110  
111 0 @PHP = split / +/,
112 $rackman->config->val(CONFIG_SECTION, "php", DEFAULT_PHP." -q");
113  
114 0 ▶ if (my $user = $rackman->config->val(CONFIG_SECTION, "sudo_as")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $rackman->config->val("format:cacti", "sudo_as")) 0 0 0
115 0 @SUDO = ( "/usr/bin/sudo", "-u", $user );
116 }
117 }
118  
119  
120 #
121 # cacti_exec()
122 # ----------
123 0 ▶sub cacti_exec {
Subroutine cacti_exec: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my ($cacti_cmd, $opts, $out) = @_;
125  
126 0 my @cacti_args = map "--$_=$opts->{$_}", keys %$opts;
127 0 my @cmd = ( @SUDO, @PHP, "$CACTI/$cacti_cmd", @cacti_args );
128 0 my ($in, $err); # note: Cacti commands do not use stderr
129 0 return IPC::Run::run(\@cmd, \$in, $out, \$err);
130 }
131  
132  
133 #
134 # cacti_add_device()
135 # ----------------
136 0 ▶sub cacti_add_device {
Subroutine cacti_add_device: not called CC=2 SCAR=17.9
Pod undocumented
137 0 my $opts = shift;
138  
139 0 my $r = cacti_exec("add_device.php", $opts, \my $out);
140 0 my ($host_id) = $out =~ /device-id: +\((\d+)\)/m;
141 0 ▶ RackMan->warning("can't obtain ID from Cacti for host ",
Branch: 1 branch
BranchTrueFalseTotal
unless $host_id 0 0 0
142 "$opts->{description}: $out") unless $host_id;
143  
144 0 return $host_id
145 }
146  
147  
148 #
149 # cacti_add_data_query()
150 # --------------------
151 0 ▶sub cacti_add_data_query {
Subroutine cacti_add_data_query: not called CC=1 SCAR=6.9
Pod undocumented
152 0 my $opts = shift;
153  
154 0 my $r = cacti_exec("add_data_query.php", $opts, \my $out);
155 }
156  
157  
158 #
159 # cacti_add_graph_template()
160 # ------------------------
161 0 ▶sub cacti_add_graph_template {
Subroutine cacti_add_graph_template: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my $opts = shift;
163  
164 0 my $r = cacti_exec("add_graph_template.php", $opts, \my $out);
165 }
166  
167  
168 #
169 # cacti_add_graphs()
170 # ----------------
171 0 ▶sub cacti_add_graphs {
Subroutine cacti_add_graphs: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my $opts = shift;
173  
174 0 my $r = cacti_exec("add_graphs.php", $opts, \my $out);
175 }
176  
177  
178 2 __PACKAGE__
179  
180 __END__
« blib/lib/RackMan/Device/PDU/APC_RackPDU.pm ↑ Summary blib/lib/RackMan/Device/Switch/Cisco_Catalyst.pm »