1#!/usr/bin/perl 2 3use DBI; 4 5# This should be the URL for the base of your cacti install (no trailing slash) 6$cacti_base = "http://www.mynet.net/cacti"; 7 8# How we should access your Cacti database.... 9$db_name = "cacti"; 10$db_username = "cactiuser"; 11$db_password = "somepassword"; 12$db_host = "localhost"; 13 14# 15# You shouldn't need to change anything below here 16# 17 18$cacti_graph = "$cacti_base/graph_image.php?local_graph_id=%d&rra_id=0&graph_nolegend=true&graph_height=100&graph_width=300"; 19$cacti_graphpage = "$cacti_base/graph.php?rra_id=all&local_graph_id=%d"; 20 21$DSN = "DBI:mysql:database=$db_name:host=$db_host"; 22 23$dbh = DBI->connect( $DSN, $db_username, $db_password ); 24 25$inputfile = $ARGV[0]; 26$outputfile = $inputfile . ".new"; 27 28open( INPUT, $inputfile ) || die($!); 29open( OUTPUT, ">$outputfile" ); 30 31while (<INPUT>) { 32 if (m/^\s*LINK\s+(\S+)/i) { 33 if ( $overlibcount == 0 && $target ne "" ) { 34 find_graph_urls($target); 35 } 36 37 $overlibcount = 0; 38 $target = ""; 39 } 40 if (m/^\s*TARGET\s+(\S+\.rrd)/i) { 41 $target = $1; 42 } 43 if (m/^\s*OVERLIBGRAPH\s+(\S+)/i) { 44 $overlibcount++; 45 } 46 47 print OUTPUT $_; 48} 49 50# for the last LINK 51if ( $overlibcount == 0 && $target ne "" ) { 52 find_graph_urls($target); 53} 54 55close(OUTPUT); 56close(INPUT); 57 58print "\nNew config file is saved in $outputfile\n"; 59 60sub find_graph_urls { 61 my ($target) = shift; 62 63 # $dbh is global 64 my ( @bits, $SQL, $sth, $data ); 65 my ( $data_template_id, $local_data_id, $count, $output ); 66 my ( $local_graph_id, $title, $graph_url, $graphpage_url ); 67 68 # we've reached the next link entry, and there's work to be done 69 @bits = split( /\//, $target ); 70 $target = $bits[-1]; 71 print "Find a graph for $target\n"; 72 73 $SQL = "select local_data_id from data_template_data where data_source_path like '%".$target."' LIMIT 1"; 74 $sth = $dbh->prepare($SQL); 75 $sth->execute(); 76 $data = $sth->fetchrow_hashref(); 77 $local_data_id = $$data{local_data_id}; 78 $sth->finish(); 79 80 $SQL = 81"SELECT id FROM data_template_rrd WHERE local_data_id=$local_data_id LIMIT 1"; 82 $sth = $dbh->prepare($SQL); 83 $sth->execute(); 84 $data = $sth->fetchrow_hashref(); 85 $data_template_rrd_id = $$data{id}; 86 $sth->finish(); 87 88 $SQL = 89"SELECT DISTINCT graph_templates_item.local_graph_id,title_cache FROM graph_templates_item,graph_templates_graph WHERE task_item_id=$data_template_rrd_id and graph_templates_graph.local_graph_id = graph_templates_item.local_graph_id"; 90 $sth = $dbh->prepare($SQL); 91 $sth->execute(); 92 $count = 0; 93 $output = ""; 94 while ( $data = $sth->fetchrow_hashref() ) { 95 $local_graph_id = $$data{local_graph_id}; 96 $title = $$data{title_cache}; 97 $graph_url = sprintf( $cacti_graph, $local_graph_id ); 98 $graphpage_url = sprintf( $cacti_graphpage, $local_graph_id ); 99 $output .= "\t# POSSIBLE OVERLIBGRAPH ($title) \n"; 100 $output .= "\t# OVERLIBGRAPH $graph_url\n"; 101 $output .= "\t# INFOURL $graphpage_url\n"; 102 $count++; 103 } 104 $sth->finish(); 105 if ( $count == 1 ) { 106 print " Single option. Adding it.\n"; 107 print OUTPUT 108 "\t#Automatically made. Graph is ID $local_graph_id: $title\n"; 109 print OUTPUT "\tOVERLIBGRAPH $graph_url\n"; 110 print OUTPUT "\tINFOURL $graphpage_url\n"; 111 } 112 else { 113 print " Multiple options. Adding them as comments.\n"; 114 print OUTPUT $output; 115 } 116 117 print OUTPUT "\n\n"; 118 119} 120