+=head1 SUBROUTINES
+
+=over 4
+
+=item process_graphs JOB PARAM
+
+Intended to be run as an FS::queue job.
+
+Copies graphs for a single service from Cacti export directory to FS cache,
+generates basic html pages for this service with base64-encoded graphs embedded,
+and stores the generated pages in the database.
+
+=back
+
+=cut
+
+sub process_graphs {
+ my ($job,$param) = @_;
+
+ $job->update_statustext(10);
+ my $cachedir = trailslash($FS::UID::cache_dir,'cache.'.$FS::UID::datasrc,'cacti-graphs');
+
+ # load the service
+ my $svcnum = $param->{'svcnum'} || die "No svcnum specified";
+ my $svc = qsearchs({
+ 'table' => 'svc_broadband',
+ 'hashref' => { 'svcnum' => $svcnum },
+ }) || die "Could not load svcnum $svcnum";
+
+ # load relevant FS::part_export::cacti object
+ my ($self) = $svc->cust_svc->part_svc->part_export('cacti');
+
+ $job->update_statustext(20);
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ # check for existing pages
+ my $now = time;
+ my @oldpages = qsearch({
+ 'table' => 'cacti_page',
+ 'hashref' => { 'svcnum' => $svcnum, 'exportnum' => $self->exportnum },
+ 'select' => 'cacti_pagenum, exportnum, svcnum, graphnum, imported', #no need to load old content
+ 'order_by' => 'ORDER BY graphnum',
+ });
+ if (@oldpages) {
+ #if pages are recent enough, do nothing and return
+ if ($oldpages[0]->imported > $self->exptime($now)) {
+ $job->update_statustext(100);
+ return '';
+ }
+ #delete old pages
+ foreach my $oldpage (@oldpages) {
+ my $error = $oldpage->delete;
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ die $error;
+ }
+ }
+ }
+
+ $job->update_statustext(30);
+
+ # get list of graphs for this svc from cacti server
+ my $cmd = $php
+ . trailslash($self->option('script_path'))
+ . q(freeside_cacti.php --get-graphs --ip=')
+ . $svc->ip_addr
+ . q(');
+ $cmd .= q( --include-path=') . $self->option('include_path') . q(')
+ if $self->option('include_path');
+ my @graphs = map { [ split(/\t/,$_) ] }
+ split(/\n/, ssh_cmd(
+ 'host' => $self->machine,
+ 'user' => $self->option('user'),
+ 'command' => $cmd
+ ));
+
+ $job->update_statustext(40);
+
+ # copy graphs from cacti server to cache
+ # requires version 2.6.4 of rsync, released March 2005
+ my $rsync = File::Rsync->new({
+ 'rsh' => 'ssh',
+ 'verbose' => 1,
+ 'recursive' => 1,
+ 'quote-src' => 1,
+ 'quote-dst' => 1,
+ 'source' => trailslash($self->option('graphs_path')),
+ 'dest' => $cachedir,
+ 'include' => [
+ (map { q('**graph_).${$_}[0].q(*.png') } @graphs),
+ (map { q('**thumb_).${$_}[0].q(.png') } @graphs),
+ q('*/'),
+ q('- *'),
+ ],
+ });
+ #don't know why a regular $rsync->exec isn't doing includes right, but this does
+ my $rscmd = join(' ',@{$rsync->getcmd()});
+ my $error = system($rscmd);
+ die "rsync ($rscmd) failed with exit status $error" if $error;
+
+ $job->update_statustext(50);
+
+ # create html file contents
+ my $svchead = q(<!-- UPDATED ) . $now . qq( -->)
+ . '<H2 STYLE="margin-top: 0;">Service #' . $svcnum . '</H2>'
+ . q(<P>Last updated ) . scalar(localtime($now)) . q(</P>);
+ my $svchtml = $svchead;
+ my $maxgraph = 1024 * 1024 * ($self->options('max_graph_size') || 5);
+ my $nographs = 1;
+ for (my $i = 0; $i <= $#graphs; $i++) {
+ my $graph = $graphs[$i];
+ my $thumbfile = $cachedir . 'graphs/thumb_' . $$graph[0] . '.png';
+ if (-e $thumbfile) {
+ if ( stat($thumbfile)->size() < $maxgraph ) {
+ $nographs = 0;
+ # add graph to main file
+ my $graphhead = q(<H3>) . $$graph[1] . q(</H3>);
+ $svchtml .= $graphhead;
+ $svchtml .= anchor_tag( $svcnum, $$graph[0], img_tag($thumbfile) );
+ # create graph details file
+ my $graphhtml = $svchead . $graphhead;
+ my $nodetail = 1;
+ my $j = 1;
+ while (-e (my $graphfile = $cachedir.'graphs/graph_'.$$graph[0].'_'.$j.'.png')) {
+ if ( stat($graphfile)->size() < $maxgraph ) {
+ $nodetail = 0;
+ $graphhtml .= img_tag($graphfile);
+ }
+ unlink($graphfile);
+ $j++;
+ }
+ $graphhtml .= '<P>No detail graphs to display for this graph</P>'
+ if $nodetail;
+ my $newobj = new FS::cacti_page {
+ 'exportnum' => $self->exportnum,
+ 'svcnum' => $svcnum,
+ 'graphnum' => $$graph[0],
+ 'imported' => $now,
+ 'content' => $graphhtml,
+ };
+ $error = $newobj->insert;
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ die $error;
+ }
+ } else {
+ $svchtml .= qq(<P STYLE="color: #FF0000">File $thumbfile is too large, skipping</P>);
+ }
+ unlink($thumbfile);
+ } else {
+ $svchtml .= qq(<P STYLE="color: #FF0000">Error loading graph: $$graph[0]</P>);
+ }
+ $job->update_statustext(49 + int($i / @graphs) * 50);
+ }
+ $svchtml .= '<P>No graphs to display for this service</P>'
+ if $nographs;
+ my $newobj = new FS::cacti_page {
+ 'exportnum' => $self->exportnum,
+ 'svcnum' => $svcnum,
+ 'graphnum' => '',
+ 'imported' => $now,
+ 'content' => $svchtml,
+ };
+ $error = $newobj->insert;
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ die $error;
+ }
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+ $job->update_statustext(100);
+ return '';
+}
+
+sub img_tag {
+ my $somefile = shift;
+ return q(<IMG SRC="data:image/png;base64,)
+ . encode_base64(slurp($somefile,binmode=>':raw'),'')
+ . qq(" STYLE="margin-bottom: 1em;"><BR>);
+}
+
+sub anchor_tag {
+ my ($svcnum, $graphnum, $contents) = @_;
+ return q(<A HREF="?svcnum=)
+ . $svcnum
+ . q(&graphnum=)
+ . $graphnum
+ . q(">)
+ . $contents
+ . q(</A>);
+}
+
+#this gets used by everything else