1 package FS::part_export::cacti;
11 Cacti integration for Freeside
15 This module in particular handles FS::part_export object creation for Cacti integration;
16 consult any existing L<FS::part_export> documentation for details on how that works.
22 use base qw( FS::part_export );
23 use FS::Record qw( qsearchs qsearch );
24 use FS::UID qw( dbh );
28 use File::Slurp qw( slurp );
30 use MIME::Base64 qw( encode_base64 );
36 tie my %options, 'Tie::IxHash',
37 'user' => { label => 'User Name',
38 default => 'freeside' },
39 'script_path' => { label => 'Script Path',
40 default => '/usr/share/cacti/cli/' },
41 'template_id' => { label => 'Host Template ID',
43 'tree_id' => { label => 'Graph Tree ID (optional)',
45 'description' => { label => 'Description (can use tokens $contact, $ip_addr and $description)',
46 default => 'Freeside $contact $description $ip_addr' },
47 'graphs_path' => { label => 'Graph Export Directory (user@host:/path/to/graphs/)',
49 'import_freq' => { label => 'Minimum minutes between graph imports',
51 'max_graph_size' => { label => 'Maximum size per graph (MB)',
53 'delete_graphs' => { label => 'Delete associated graphs and data sources when unprovisioning',
56 'include_path' => { label => 'Path to cacti include dir (relative to script_path)',
57 default => '../site/include/' },
58 'cacti_graph_template_id' => {
59 'label' => 'Graph Template',
63 'cacti_snmp_query_id' => {
64 'label' => 'SNMP Query ID',
68 'cacti_snmp_query_type_id' => {
69 'label' => 'SNMP Query Type ID',
73 'cacti_snmp_field' => {
74 'label' => 'SNMP Field',
78 'cacti_snmp_value' => {
79 'label' => 'SNMP Value',
86 'svc' => 'svc_broadband',
87 'desc' => 'Export service to cacti server, for svc_broadband services',
88 'post_config_element' => '/edit/elements/part_export/cacti.html',
89 'options' => \%options,
91 Add service to cacti upon provisioning, for broadband services.<BR>
92 See <A HREF="http://www.freeside.biz/mediawiki/index.php/Freeside:4:Documentation:Cacti#Connecting_Cacti_To_Freeside">documentation</A> for details.
96 # standard hooks for provisioning/unprovisioning service
99 my ($self, $svc_broadband) = @_;
100 my ($q,$error) = _insert_queue($self, $svc_broadband);
105 my ($self, $svc_broadband) = @_;
106 my $oldAutoCommit = $FS::UID::AutoCommit;
107 local $FS::UID::AutoCommit = 0;
109 foreach my $page (qsearch('cacti_page',{ svcnum => $svc_broadband->svcnum })) {
110 my $error = $page->delete;
112 $dbh->rollback if $oldAutoCommit;
116 my ($q,$error) = _delete_queue($self, $svc_broadband);
118 $dbh->rollback if $oldAutoCommit;
121 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
125 sub _export_replace {
126 my($self, $new, $old) = @_;
127 return '' if $new->ip_addr eq $old->ip_addr; #important part didn't change
128 #delete old then insert new, with second job dependant on the first
129 my $oldAutoCommit = $FS::UID::AutoCommit;
130 local $FS::UID::AutoCommit = 0;
132 my ($dq, $iq, $error);
133 ($dq,$error) = _delete_queue($self,$old);
135 $dbh->rollback if $oldAutoCommit;
138 ($iq,$error) = _insert_queue($self,$new);
140 $dbh->rollback if $oldAutoCommit;
143 $error = $iq->depend_insert($dq->jobnum);
145 $dbh->rollback if $oldAutoCommit;
148 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
152 sub _export_suspend {
156 sub _export_unsuspend {
163 my ($self, $svc_broadband) = @_;
164 my $queue = new FS::queue {
165 'svcnum' => $svc_broadband->svcnum,
166 'job' => "FS::part_export::cacti::ssh_insert",
168 my $error = $queue->insert(
169 'host' => $self->machine,
170 'user' => $self->option('user'),
171 'hostname' => $svc_broadband->ip_addr,
172 'script_path' => $self->option('script_path'),
173 'template_id' => $self->option('template_id'),
174 'tree_id' => $self->option('tree_id'),
175 'description' => $self->option('description'),
176 'svc_desc' => $svc_broadband->description,
177 'contact' => $svc_broadband->cust_main->contact,
178 'svcnum' => $svc_broadband->svcnum,
181 return ($queue,$error);
185 my ($self, $svc_broadband) = @_;
186 my $queue = new FS::queue {
187 'svcnum' => $svc_broadband->svcnum,
188 'job' => "FS::part_export::cacti::ssh_delete",
190 my $error = $queue->insert(
191 'host' => $self->machine,
192 'user' => $self->option('user'),
193 'hostname' => $svc_broadband->ip_addr,
194 'script_path' => $self->option('script_path'),
195 'delete_graphs' => $self->option('delete_graphs'),
196 'include_path' => $self->option('include_path'),
198 return ($queue,$error);
201 # routines run by queued jobs
205 my $self = $opt{'self'};
208 die "Non-numerical Host Template ID, check export configuration\n"
209 unless $opt{'template_id'} =~ /^\d+$/;
210 die "Non-numerical Graph Tree ID, check export configuration\n"
211 unless $opt{'tree_id'} =~ /^\d*$/;
214 my $desc = $opt{'description'};
215 $desc =~ s/\$ip_addr/$opt{'hostname'}/g;
216 $desc =~ s/\$description/$opt{'svc_desc'}/g;
217 $desc =~ s/\$contact/$opt{'contact'}/g;
218 #for some reason, device names with apostrophes fail to export graphs in Cacti
219 #just removing them for now, someday maybe dig to figure out why
220 # $desc =~ s/'/'\\''/g;
223 . trailslash($opt{'script_path'})
224 . q(add_device.php --description=')
229 . $opt{'template_id'};
230 my $response = ssh_cmd(%opt, 'command' => $cmd);
231 unless ( $response =~ /Success - new device-id: \((\d+)\)/ ) {
232 die "Error adding device: $response";
237 if ($opt{'tree_id'}) {
239 . trailslash($opt{'script_path'})
240 . q(add_tree.php --type=node --node-type=host --tree-id=)
244 $response = ssh_cmd(%opt, 'command' => $cmd);
245 unless ( $response =~ /Added Node node-id: \((\d+)\)/ ) {
246 die "Host added, but error adding host to tree: $response";
250 # Get list of graph templates for new id
252 . trailslash($opt{'script_path'})
253 . q(freeside_cacti.php --get-graph-templates --host-template=)
254 . $opt{'template_id'};
255 $cmd .= q( --include-path=') . $self->option('include_path') . q(')
256 if $self->option('include_path');
257 my $ginfo = { map { $_ ? ($_ => undef) : () } split(/\n/,ssh_cmd(%opt, 'command' => $cmd)) };
259 # Add extra config info
260 my @xtragid = split("\n", $self->option('cacti_graph_template_id'));
261 my @query_id = split("\n", $self->option('cacti_snmp_query_id'));
262 my @query_type_id = split("\n", $self->option('cacti_snmp_query_type_id'));
263 my @snmp_field = split("\n", $self->option('cacti_snmp_field'));
264 my @snmp_value = split("\n", $self->option('cacti_snmp_value'));
265 for (my $i = 0; $i < @xtragid; $i++) {
266 my $gtid = $xtragid[$i];
267 $ginfo->{$gtid} ||= [];
268 push(@{$ginfo->{$gtid}},{
270 'query_id' => $query_id[$i],
271 'query_type_id' => $query_type_id[$i],
272 'snmp_field' => $snmp_field[$i],
273 'snmp_value' => $snmp_value[$i],
278 ref($ginfo->{$_}) ? @{$ginfo->{$_}} : {'gtid' => $_}
280 warn "Host ".$opt{'hostname'}." exported to cacti, but no graphs configured"
285 foreach my $gdef (@gdefs) {
286 # validate graph info
287 my $gtid = $gdef->{'gtid'};
289 $gerror .= " Bad graph template: $gtid"
290 unless $gtid =~ /^\d+$/;
291 my $isds = $gdef->{'query_id'}
292 || $gdef->{'query_type_id'}
293 || $gdef->{'snmp_field'}
294 || $gdef->{'snmp_value'};
296 $gerror .= " Bad SNMP Query Id: " . $gdef->{'query_id'}
297 unless $gdef->{'query_id'} =~ /^\d+$/;
298 $gerror .= " Bad SNMP Query Type Id: " . $gdef->{'query_type_id'}
299 unless $gdef->{'query_type_id'} =~ /^\d+$/;
300 $gerror .= " SNMP Field cannot contain apostrophe"
301 if $gdef->{'snmp_field'} =~ /'/;
302 $gerror .= " SNMP Value cannot contain apostrophe"
303 if $gdef->{'snmp_value'} =~ /'/;
309 . trailslash($opt{'script_path'})
310 . q(add_graphs.php --graph-type=)
311 . ($isds ? 'ds' : 'cg')
312 . q( --graph-template-id=)
317 $cmd .= q( --snmp-query-id=)
318 . $gdef->{'query_id'}
319 . q( --snmp-query-type-id=)
320 . $gdef->{'query_type_id'}
322 . $gdef->{'snmp_field'}
323 . q(' --snmp-value=')
324 . $gdef->{'snmp_value'}
327 $response = ssh_cmd(%opt, 'command' => $cmd);
328 #might be more than one graph added, just testing success
329 $gerror .= "Error creating graph $gtid: $response"
330 unless $response =~ /Graph Added - graph-id: \((\d+)\)/;
334 # job fails, but partial export may have occurred
335 die $gerror . " Partial export occurred\n" if $gerror;
343 . trailslash($opt{'script_path'})
344 . q(freeside_cacti.php --drop-device --ip=')
347 $cmd .= q( --delete-graphs)
348 if $opt{'delete_graphs'};
349 $cmd .= q( --include-path=') . $opt{'include_path'} . q(')
350 if $opt{'include_path'};
351 my $response = ssh_cmd(%opt, 'command' => $cmd);
352 die "Error removing from cacti: " . $response
361 =item process_graphs JOB PARAM
363 Intended to be run as an FS::queue job.
365 Copies graphs for a single service from Cacti export directory to FS cache,
366 generates basic html pages for this service with base64-encoded graphs embedded,
367 and stores the generated pages in the database.
374 my ($job,$param) = @_;
376 $job->update_statustext(10);
377 my $cachedir = trailslash($FS::UID::cache_dir,'cache.'.$FS::UID::datasrc,'cacti-graphs');
380 my $svcnum = $param->{'svcnum'} || die "No svcnum specified";
382 'table' => 'svc_broadband',
383 'hashref' => { 'svcnum' => $svcnum },
384 }) || die "Could not load svcnum $svcnum";
386 # load relevant FS::part_export::cacti object
387 my ($self) = $svc->cust_svc->part_svc->part_export('cacti');
389 $job->update_statustext(20);
391 my $oldAutoCommit = $FS::UID::AutoCommit;
392 local $FS::UID::AutoCommit = 0;
395 # check for existing pages
397 my %oldpages = map { ($_->graphnum || 'MAIN') => $_ } qsearch({
398 'table' => 'cacti_page',
399 'hashref' => { 'svcnum' => $svcnum, 'exportnum' => $self->exportnum },
400 'select' => 'cacti_pagenum, exportnum, svcnum, graphnum, imported, thumbnail', #no need to load old content
401 'order_by' => 'ORDER BY graphnum',
404 # if all existing pages are recent enough, do nothing and return
405 # (won't detect newly introduced graphs, but they can wait for next run)
407 if (keys %oldpages) {
409 foreach my $oldpage (keys %oldpages) {
410 if ($oldpages{$oldpage}->imported <= $self->exptime($now)) {
417 $job->update_statustext(100);
421 $job->update_statustext(30);
423 # get list of graphs for this svc from cacti server
425 . trailslash($self->option('script_path'))
426 . q(freeside_cacti.php --get-graphs --ip=')
429 $cmd .= q( --include-path=') . $self->option('include_path') . q(')
430 if $self->option('include_path');
431 my @graphs = map { [ split(/\t/,$_) ] }
433 'host' => $self->machine,
434 'user' => $self->option('user'),
438 $job->update_statustext(40);
440 # copy graphs from cacti server to cache
441 # requires version 2.6.4 of rsync, released March 2005
442 my $rsync = File::Rsync->new({
448 'source' => trailslash($self->option('graphs_path')),
451 (map { q('**graph_).${$_}[0].q(*.png') } @graphs),
452 (map { q('**thumb_).${$_}[0].q(.png') } @graphs),
457 #don't know why a regular $rsync->exec isn't doing includes right, but this does
458 my $rscmd = join(' ',@{$rsync->getcmd()});
459 my $error = system($rscmd);
460 die "rsync ($rscmd) failed with exit status $error" if $error;
462 $job->update_statustext(50);
464 # create html file contents
465 my $svchead = q(<!-- UPDATED ) . $now . qq( -->)
466 . '<H2 STYLE="margin-top: 0;">Service #' . $svcnum . '</H2>'
467 . q(<P>Last updated ) . scalar(localtime($now)) . q(</P>);
468 my $svchtml = $svchead;
469 my $maxgraph = 1024 * 1024 * ($self->options('max_graph_size') || 5);
471 for (my $i = 0; $i <= $#graphs; $i++) {
472 my $graph = $graphs[$i];
473 my $thumbfile = $cachedir . 'graphs/thumb_' . $$graph[0] . '.png';
475 if ( stat($thumbfile)->size() < $maxgraph ) {
477 my $thumbnail = img_tag($thumbfile);
478 # add graph to main file
479 my $graphhead = q(<H3>) . $$graph[1] . q(</H3>);
480 $svchtml .= $graphhead;
481 $svchtml .= anchor_tag( $svcnum, $$graph[0], $thumbnail );
482 # create graph details file
483 my $graphhtml = $svchead . $graphhead;
486 # no easy way to tell what detail graphs should exist,
487 # and don't want detail graphs that are out of sync with thumbnail,
488 # so just use what we can find
489 while (-e (my $graphfile = $cachedir.'graphs/graph_'.$$graph[0].'_'.$j.'.png')) {
490 if ( stat($graphfile)->size() < $maxgraph ) {
492 $graphhtml .= img_tag($graphfile);
497 $graphhtml .= '<P>No detail graphs to display for this graph</P>'
499 #delete old detail page
500 if ($oldpages{$$graph[0]}) {
501 $error = $oldpages{$$graph[0]}->delete;
503 $dbh->rollback if $oldAutoCommit;
507 #insert new detail page
508 my $newobj = new FS::cacti_page {
509 'exportnum' => $self->exportnum,
511 'graphnum' => $$graph[0],
513 'content' => $graphhtml,
514 'thumbnail' => $thumbnail,
516 $error = $newobj->insert;
518 $dbh->rollback if $oldAutoCommit;
522 $svchtml .= qq(<P STYLE="color: #FF0000">File $thumbfile is too large, skipping</P>);
526 # try to use old page for this graph
527 if ($oldpages{$$graph[0]} && $oldpages{$$graph[0]}->thumbnail) {
529 # add old graph to main file
530 my $graphhead = q(<H3>) . $$graph[1] . q(</H3>);
531 $svchtml .= $graphhead;
532 $svchtml .= qq(<P STYLE="color: #FF0000">Current graphs unavailable; using previously imported data.</P>);
533 $svchtml .= anchor_tag( $svcnum, $$graph[0], $oldpages{$$graph[0]}->thumbnail );
535 $svchtml .= qq(<P STYLE="color: #FF0000">Error loading graph: $$graph[0]</P>);
538 # remove old page from hash even if it is being reused,
539 # remaining entries in hash will be deleted from database below
540 delete $oldpages{$$graph[0]} if $oldpages{$$graph[0]};
541 $job->update_statustext(49 + int($i / @graphs) * 50);
543 $svchtml .= '<P>No graphs to display for this service</P>'
545 # delete remaining old pages, including svc index
546 foreach my $oldpage (keys %oldpages) {
547 $error = $oldpages{$oldpage}->delete;
549 $dbh->rollback if $oldAutoCommit;
553 # insert new index page for svc
554 my $newobj = new FS::cacti_page {
555 'exportnum' => $self->exportnum,
559 'content' => $svchtml,
562 $error = $newobj->insert;
564 $dbh->rollback if $oldAutoCommit;
568 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
570 $job->update_statustext(100);
575 my $somefile = shift;
576 return q(<IMG SRC="data:image/png;base64,)
577 . encode_base64(slurp($somefile,binmode=>':raw'),'')
578 . qq(" STYLE="margin-bottom: 1em;"><BR>);
582 my ($svcnum, $graphnum, $contents) = @_;
583 return q(<A HREF="?svcnum=)
592 #this gets used by everything else
593 #fake false laziness, other ssh_cmds handle error/output differently
597 my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'});
598 die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
599 my ($output, $errput) = $ssh->capture2($opt->{'command'});
600 die "Error running SSH command: ". $opt->{'command'}. ' ERROR: ' . $ssh->error if $ssh->error;
601 die $errput if $errput;
605 #there's probably a better place to put this?
606 #makes sure there's a trailing slash between/after input
607 #doesn't add leading slashes
611 foreach my $path (@paths) {
613 $out .= '/' unless $out =~ /\/$/;
624 Removes all expired graphs for this export from the database.
630 my $oldAutoCommit = $FS::UID::AutoCommit;
631 local $FS::UID::AutoCommit = 0;
633 my $sth = $dbh->prepare('DELETE FROM cacti_page WHERE exportnum = ? and imported <= ?')
635 $dbh->rollback if $oldAutoCommit;
638 $sth->execute($self->exportnum,$self->exptime)
640 $dbh->rollback if $oldAutoCommit;
643 $dbh->commit or return $dbh->errstr if $oldAutoCommit;
647 =item exptime [ TIME ]
649 Accepts optional current time, defaults to actual current time.
651 Returns timestamp for the oldest possible non-expired graph import,
652 based on the import_freq option.
658 my $now = shift || time;
659 return $now - 60 * ($self->option('import_freq') || 5);
667 jonathan@freeside.biz