-# # Get list of graph templates for new id
-# $cmd = $php
-# . $opt{'script_path'}
-# . q(freeside_cacti.php --get-graph-templates --host-template=)
-# . $opt{'template_id'};
-# my @gtids = split(/\n/,ssh_cmd(%opt, 'command' => $cmd));
-# die "No graphs configured for host template"
-# unless @gtids;
-#
-# # Create graphs
-# foreach my $gtid (@gtids) {
-#
-# # sanity checks, should never happen
-# next unless $gtid;
-# die "Bad graph template: $gtid"
-# unless $gtid =~ /^\d+$/;
-#
-# # create the graph
-# $cmd = $php
-# . $opt{'script_path'}
-# . q(add_graphs.php --graph-type=cg --graph-template-id=)
-# . $gtid
-# . q( --host-id=)
-# . $id;
-# $response = ssh_cmd(%opt, 'command' => $cmd);
-# die "Error creating graph $gtid: $response"
-# unless $response =~ /Graph Added - graph-id: \((\d+)\)/;
-# my $gid = $1;
-#
-# # add the graph to the tree
-# $cmd = $php
-# . $opt{'script_path'}
-# . q(add_tree.php --type=node --node-type=graph --tree-id=)
-# . $opt{'tree_id'}
-# . q( --graph-id=)
-# . $gid;
-# $response = ssh_cmd(%opt, 'command' => $cmd);
-# die "Error adding graph $gid to tree: $response"
-# unless $response =~ /Added Node/;
-#
-# } #foreach $gtid
+ # Get list of graph templates for new id
+ $cmd = $php
+ . trailslash($opt{'script_path'})
+ . q(freeside_cacti.php --get-graph-templates --host-template=)
+ . $opt{'template_id'};
+ $cmd .= q( --include-path=') . $self->option('include_path') . q(')
+ if $self->option('include_path');
+ my $ginfo = { map { $_ ? ($_ => undef) : () } split(/\n/,ssh_cmd(%opt, 'command' => $cmd)) };
+
+ # Add extra config info
+ my @xtragid = split("\n", $self->option('cacti_graph_template_id'));
+ my @query_id = split("\n", $self->option('cacti_snmp_query_id'));
+ my @query_type_id = split("\n", $self->option('cacti_snmp_query_type_id'));
+ my @snmp_field = split("\n", $self->option('cacti_snmp_field'));
+ my @snmp_value = split("\n", $self->option('cacti_snmp_value'));
+ for (my $i = 0; $i < @xtragid; $i++) {
+ my $gtid = $xtragid[$i];
+ $ginfo->{$gtid} ||= [];
+ push(@{$ginfo->{$gtid}},{
+ 'gtid' => $gtid,
+ 'query_id' => $query_id[$i],
+ 'query_type_id' => $query_type_id[$i],
+ 'snmp_field' => $snmp_field[$i],
+ 'snmp_value' => $snmp_value[$i],
+ });
+ }
+
+ my @gdefs = map {
+ ref($ginfo->{$_}) ? @{$ginfo->{$_}} : {'gtid' => $_}
+ } keys %$ginfo;
+ warn "Host ".$opt{'hostname'}." exported to cacti, but no graphs configured"
+ unless @gdefs;
+
+ # Create graphs
+ my $gerror = '';
+ foreach my $gdef (@gdefs) {
+ # validate graph info
+ my $gtid = $gdef->{'gtid'};
+ next unless $gtid;
+ $gerror .= " Bad graph template: $gtid"
+ unless $gtid =~ /^\d+$/;
+ my $isds = $gdef->{'query_id'}
+ || $gdef->{'query_type_id'}
+ || $gdef->{'snmp_field'}
+ || $gdef->{'snmp_value'};
+ if ($isds) {
+ $gerror .= " Bad SNMP Query Id: " . $gdef->{'query_id'}
+ unless $gdef->{'query_id'} =~ /^\d+$/;
+ $gerror .= " Bad SNMP Query Type Id: " . $gdef->{'query_type_id'}
+ unless $gdef->{'query_type_id'} =~ /^\d+$/;
+ $gerror .= " SNMP Field cannot contain apostrophe"
+ if $gdef->{'snmp_field'} =~ /'/;
+ $gerror .= " SNMP Value cannot contain apostrophe"
+ if $gdef->{'snmp_value'} =~ /'/;
+ }
+ next if $gerror;
+
+ # create the graph
+ $cmd = $php
+ . trailslash($opt{'script_path'})
+ . q(add_graphs.php --graph-type=)
+ . ($isds ? 'ds' : 'cg')
+ . q( --graph-template-id=)
+ . $gtid
+ . q( --host-id=)
+ . $id;
+ if ($isds) {
+ $cmd .= q( --snmp-query-id=)
+ . $gdef->{'query_id'}
+ . q( --snmp-query-type-id=)
+ . $gdef->{'query_type_id'}
+ . q( --snmp-field=')
+ . $gdef->{'snmp_field'}
+ . q(' --snmp-value=')
+ . $gdef->{'snmp_value'}
+ . q(');
+ }
+ $response = ssh_cmd(%opt, 'command' => $cmd);
+ #might be more than one graph added, just testing success
+ $gerror .= "Error creating graph $gtid: $response"
+ unless $response =~ /Graph Added - graph-id: \((\d+)\)/;
+
+ } #foreach $gtid
+
+ # job fails, but partial export may have occurred
+ die $gerror . " Partial export occurred\n" if $gerror;