#!/usr/bin/perl use strict; use warnings; use RRD::Simple (); use Data::Dumper; $| = 1; # Flush the output my ($unixtime,$rrd); my $file = "perl.txt"; my $path = '/home/tiny_os/Desktop/Test_Perl/'; my $period = '3years'; my $rrdfile = 'myfile'; while (sleep 1) { open(FH, ">>", $file) || die "Unable to open $file: $!\n"; my $range = 50; my $minimum = 100; my $random_number_in = int(rand($range)) + $minimum; my $random_number_out = int(rand($range)) + $minimum; my $random_number_sec = int(rand($range)) + $minimum; $rrd = RRD::Simple->new( file => "".$rrdfile."", rrdtool => "/usr/bin/rrdtool", #optional /usr/bin/rrdtool tmpdir => "/tmp", #optional cf => [ qw( AVERAGE MIN MAX LAST ) ], #optional default_dstype => "COUNTER", #optional on_missing_ds => "add", #optional ); =comment # Create an interface object $rrd = RRD::Simple->new( file => $rrdfile, cf => [qw( AVERAGE MIN MAX LAST )], #default_dstype => "DERIVE", ); =cut # Create a new RRD file with 3 data sources called # bytesIn, bytesOut and faultsPerSec. unless (-e $rrdfile) { $rrd->create( $period, bytesIn => "COUNTER", bytesOut => "COUNTER", faultsPerSec => "COUNTER" ); RRDs::tune("".$rrdfile.".rrd", "-i", "Source_Name:0"), #optional -i or --minimum RRDs::tune("".$rrdfile.".rrd", "-a", "Source_Name:4294967295") #optional -a or --maximum } =comment 32-bit = 2^32-1 = 4294967295 (counter ticks) 64-bit = 2^64-1 = 18446744073709551615 (counter ticks) 64-bit counter (caution!!!) different oid's // ifInOctets (OID: 1.3.6.1.2.1.2.2.1.10) // ifOutOctets (OID: 1.3.6.1.2.1.2.2.1.16) // sysUpTime (OID: 1.3.6.1.2.1.1.3) =cut # Put some arbitary data values in the RRD file for the same # 3 data sources called bytesIn, bytesOut and faultsPerSec. $rrd->update( bytesIn => $random_number_in, bytesOut => $random_number_out, faultsPerSec => $random_number_sec ); print FH "This is the bytes_in: $random_number_in\n"; print FH "This is the bytes_out: $random_number_out\n"; print FH "This is the bytes_sec: $random_number_sec\n"; # Generate graphs: # /home/tiny_os/Desktop/Test_Perl/myfile-hourly.png, /home/tiny_os/Desktop/Test_Perl/myfile-daily.png # /home/tiny_os/Desktop/Test_Perl/myfile-weekly.png, /home/tiny_os/Desktop/Test_Perl/myfile-monthly.png my %rtn = $rrd->graph( $rrdfile, destination => $path, basename => "".$rrdfile."_graph", timestamp => "both", # graph, rrd, both or none periods => [qw(hour day week month)], # omit to generate all graphs sources => [qw(bytesIn bytesOut faultsPerSec)], source_colors => [qw(9932CC 0000CD FFFF00)], source_labels => [("Bytes In", "Bytes Out", "Faults Per Second")], source_drawtypes => [qw(LINE LINE LINE)], # AREA , LINE, LINE1 line_thickness => 2, extended_legend => 1, title => "Network Interface eth0", vertical_label => "Bytes/Faults", width => 800, height => 500, interlaced => "", # If images are interlaced they become visible to browsers more quickly ); #printf("Created %s\n", join(", ", map { $rtn{$_}->[0] } keys %rtn)); # Return information about an RRD file my $info = $rrd->info($rrdfile); # This method will return a complex data structure containing details about the RRD file, including RRA and data source information. #print Data::Dumper::Dumper($info); my @sources = $rrd->sources($rrdfile); my $seconds = $rrd->retention_period($rrdfile); # This method will return the maximum period of time (in seconds) that the RRD file will store data for. # Get unixtime of when RRD file was last updated $unixtime = $rrd->last($rrdfile); print FH "".$rrdfile.".rrd was last updated at " . scalar(localtime($unixtime)) . "\n"; # Get list of data source names from an RRD file my @dsnames = $rrd->sources; print "Available data sources: " . join(", ", @dsnames) . "\n"; my $heartbeat_In = $rrd->heartbeat($rrdfile, "bytesIn"); # Define the name to use the heartbeat my $heartbeat_Out = $rrd->heartbeat($rrdfile, "bytesOut"); my $heartbeat_sec = $rrd->heartbeat($rrdfile, "faultsPerSec"); # This method will return the current heartbeat of a data source. printf "This is the heartbeat_in: %s\n", $heartbeat_In; my @rtn_In = $rrd->heartbeat($rrdfile, "bytesIn", 10); # Based on the predifined name set the step my @rtn_Out = $rrd->heartbeat($rrdfile, "bytesOut", 10); my @rtn_sec = $rrd->heartbeat($rrdfile, "faultsPerSec", 10); # This method will set a new heartbeat of a data source. close(FH); }