Men det gode er, Jesper, at kommer man fra Unix, hvor der står \"/\" accepteres det fuldt ud i Windows - på ligefod med \"\\\" - i hvert fald fra programmer.
Det er kun på kommandolinier i COMMAND.COM at det er et krav.
Man skal nu alligevel være opmærksom, hvis man har kode, der manipulerer filnavne - det sker selvfølgelig ikke for hvemsomhelst, men man kan nårsomhelst komme ud for moduler, der indeholder skjulte antagelser om, hvordan filnavne ser ud.
Ok, så lang så godt. Kender I MRTG til at overvåge blandt andet router traffik?
Mit problem er at man kan angive et eksternt script som leverer data tilbage til MRTG. Begge scripts er lavet af andre og jeg kender intet til Perl. Det script jeg vil bruge sammen med MRTG er til at overvåge MySQL med.
Det virker på unix, men under windows 2000 får MRTG ingen valide data retur fra det eksterne script.
Det pudsige ved dette script er at kører jeg det manuelt fra et command prompt så kan jeg se at det returnere det rigtige resultat men så snart det kaldes fra MRTG så får MRTG tilsyneladende ikke opfanget det print der skrives til STDOUT.
Her er scriptet i sin fulde lænmgde:
#!/usr/local/bin/perl -w ################################################################## # $Id: mrtg-mysql-load,v 1.8 2001/08/07 15:49:20 carsten Exp $ # # Copyright (c) 2001 Carsten H. Pedersen <carsten.pedersen@bitbybit.dk>. # All Rights Reserved. # # Losely based on mrtg-ping-probe 1.2 Copyright (c) 1997-2001 # Peter W. Osel <pwo@guug.de>. # # Copyright Carsten H. Pedersen <carsten.pedersen@bitbybit.dk> 2001. # This file is released under the GNU General Public License. # See file COPYRIGHT for details # ################################################################## require 5.003; use Getopt::Std; use File::Basename; use Config;
sub dbg_print; sub read_config; sub get_questions; sub get_slow_queries; sub get_uptime; sub get_version; sub write_output;
# prevent warnings for command-line variables which # Perl thinks we only use once. $opt_o = \"\"; $opt_v = \"\";
$Prog_name = basename($0); # Who I am
# Usage message
$Usage = \"$Prog_name: Outputs the number of questions and slow queries since last restart of the MySQL server. The output is formatted according to mrtg requirements.
Options -h, -P, -u and -p are equivalent (and map directly) to the same options for mysqladmin. -l: Write a copy of the output to file filename -t: Wait this many seconds before timeout -c: Read all the options from config file. Command line flags override options in config file. -o: Print command and options used when calling mysqladmin; exit -d: Print lots of ugly debugging messages to STDERR -v: Output version info; exit \"; # end $Usage
# Version message
$version = \"0.92-beta\"; $Version = \"mrtg-mysql-load v. $version Copyright (c) 2001 Carsten H. Pedersen <carsten.pedersen\\@bitbybit.dk>. All Rights Reserved. \"; # end version
# Parse Command Line: if (!getopts(\'h:P:u:p:t:c:l:dvo\')) { $output = $Usage; write_output; die $output; }
# if debug is turned on, tell us so if ($opt_d) { dbg_print \"Debugging is on\"; }
dbg_print \"Done parsing command line\";
# if -v: Echo version info and exit if ($opt_v) { dbg_print \"Version info requested\"; print $Version; exit(0); } else { dbg_print \"Version info not requested\"; }
# reset all variables to known values $host = \"\"; $port = \"\"; $username = \"\"; $password = \"\"; $timeout = \"\"; $logfile = \"\";
# If the config file has been specified, read the options from that # first. if ($opt_c) { read_config($opt_c); } else { dbg_print \"Config file NOT specified\"; }
# Other command line options. They may already # have been specified in a config file, but we # override if they are defined on the command line. $host = defined($opt_h) ? $opt_h : $host; $port = defined($opt_P) ? $opt_P : $port; $username = defined($opt_u) ? $opt_u : $username; $password = defined($opt_p) ? $opt_p : $password; $timeout = defined($opt_t) ? $opt_t : $timeout; $logfile = defined($opt_l) ? $opt_l : $logfile;
# construct command used for calling mysqladmin $cmd = \"mysqladmin \". ($host ? \"-h $host \" : \"\"). ($port ? \"-P $port \" : \"\"). ($username ? \"-u $username \" : \"\"). ($password ? \"-p$password \" : \"\"). # there seems to be problems with the -t option (?) # ($timeout ? \"-O connect_timeout=$timeout \" : \"\"). ($timeout ? \"-t $timeout \" : \"\").
# Note that we use the \"version\" command rather than # the \"status\" command, as version returns the same # status info plus the version number, albeit formatted # somewhat differently. As a plus, it also returns the # uptime info in a much more human-readable format. \"version \".
# we need to redirect stderr to catch error messages \"2>&1 \". \"\";
# If command line option -o is given, display $cmd and exit if ($opt_o) { print \"$cmd\\n\"; exit(0); }
$mysqladmin_output = join(\'\', <MYSQLA>); close MYSQLA;
# A better error handling could probably be implemented. # For now, we simply check whether mysqladmin returns the # string \"mysqladmin: \" as the first part of either stderr # or stdout if an error occurs. In that case, we assume # that something odd happened.
if ($mysqladmin_output =~ /^.mysqladmin:/) { $output = \"ERROR: mysqladmin returned an error message:\\n$mysqladmin_output\"; write_output; die $output; }
# Now we have all the data we need to output to mrtg:
# The external mrtg probe returns up to 4 lines of output: # 1. Line: current state of the \'incoming bytes counter\' # 2. Line: current state of the \'outgoing bytes counter\' # 3. Line: string, telling the uptime of the target. # 4. Line: telling the name of the target.
# dbg_print: Print string to STDERR if debug is turned on
sub dbg_print { if ($opt_d) { print STDERR \"$Prog_name-dbg: \"; print STDERR \"@_\\n\"; } }
# read_config: Attempt to read in the configuration file and set # variables accordingly. The format of the config file is: # # - comment, ignored # var=value<nl> # where var is one of \'host\', \'port\', \'username\', \'password\' or # \'timeout\' and value is the corresponding value, no quotation
sub read_config { my $configfile = $_[0]; my $lnum = 0;
if (!open(CFG, \"<$configfile\")) { $output = \"ERROR: Unable to read config file $configfile\"; write_output; die $output; } dbg_print(\"Reading config from file $configfile\"); for $line (<CFG>) { $lnum++; chomp $line; if ($line=~/(host|port|username|password|timeout|logfile)=(.{1,})/ ) { dbg_print(\"Got value $2 for parameter $1\"); $ {$1} = $2; } else { # a comment or an error? if ($line =~ /^#/ ) { dbg_print \"Ignored comment: $line\"; } else { print STDERR \"$Prog_name: Error: I don\'t understand line \". \"$lnum of $configfile\\n\"; exit(1); } } } dbg_print(\"Done reading config file\"); }
# get_questions: Extract \"Questions: NNNN\" from $mysqladmin_output and # tie the value to $questions
sub get_questions { if ($mysqladmin_output !~ /Questions: ([0-9]+)/ ) { $output = \"ERROR: could not find match for \'Questions:\' in \". \"mysqladmin output\\n\\n:$mysqladmin_output\"; write_output; die $output; } dbg_print \"Questions: $1\"; $questions = $1; }
# get_slow_queries: Extract \"Questions: NNNN\" from $mysqladmin_output and # tie the value to $slow_queries
sub get_slow_queries { if ($mysqladmin_output !~ /Slow queries: ([0-9]+)/ ) { $output = \"ERROR: could not find match for \'slow queries:\' in \". \"mysqladmin output\\n\\n:$mysqladmin_output\"; write_output; die $output; } dbg_print \"Slow queries: $1\"; $slow_queries = $1; }
# get_uptime: Extract \"Uptime:[\\t\\ ]+dd days hh hours mm min ss sec # from $mysqladmin_output, convert it all to seconds, and tie the # final value to $version
sub get_uptime { # Catch \"xx day(s) xx hour(s) xx min xx sec\" line from output. # mysqladmin outputs no data for days, hours, minutes until the # values is at least 1. if ($mysqladmin_output !~ /Uptime:\\s+(\\d+.+)/ ) { $output = \"ERROR: could not find match for \'Uptime:\' in \". \"mysqladmin output\\n\\n:$mysqladmin_output\"; write_output; die $output; } $uptime_s = $1; dbg_print \"Caught Uptime line: \'$uptime_s\'\";
$uptime = $1; }
# get_version: Extract \"Server version[\\t\\ ]+x.yy.zz ... \\n\" from # $mysqladmin_output and tie the value to $version
sub get_version { if ($mysqladmin_output !~ /Server version[ \\t]+([^\\n]+)/ ) { $output = \"ERROR: could find match for \'Server version\' in\\n\". \"mysqladmin output\\n\\n:$mysqladmin_output\"; write_output; die $output; } dbg_print \"Version: $1\"; $version = \"MySQL version $1\"; }
# write_output: write $output to stdout. If a logfile has been specified, # output to that as well. In this implementation, it\'s expected that # writing of output happens only once during the execution of the program.
sub write_output { # If we are to write to a log file, do it now. # The format of the log file is -- (dash-dash), timestamp, --, <nl>, # the output, <nl> if ($logfile) { ($t_sec, $t_min, $t_hour, $t_mday, $t_mon, $t_year, $t_wday, $t_yday, $t_isdst) = localtime(time); unless (open(LOGFILE, \">>$logfile\")) { print STDERR \"${Prog_name}: \". \"ERROR: Can\'t open logfile $logfile: $!\"; exit(1); } $t_year += 1900; print LOGFILE \"--$t_year-$t_mon-$t_mday $t_hour:$t_min:$t_sec--\\n\"; print LOGFILE \"$output\\n\"; close LOGFILE; # hmmm... need to do something with these in order # not to get warnings... $t_wday = 0; $t_yday = 0; $t_isdst = 0; }
Og den graf passer simpelt hen ikke med virkligheden! For det første er slow queries forkert, for det andet er mit website endnu ikke så velbesøgt at der konstant er en vis load hele døgnet.
Ja i logfilen står resultatet fint nok som det skal med linie skift og det hele. Jeg har prøvet at ændre \\n til \\r\\n efter råd fra programmøren men det hjalp ikke.
Problemet var måden scriptet kaldes på. På windows skal man angive perl når man kalder et eksternt perl script. (Sikkert noget der er en selvfølge for erfarne perl programmører)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.