WCM - Das österreichische Computer Magazin Forenübersicht
 

Zurück   WCM Forum > Rat & Tat > Programmierung

Programmierung Rat & Tat für Programmierer

Microsoft KARRIERECAMPUS

Antwort
 
Themen-Optionen Ansicht
Alt 09.03.2004, 09:19   #1
spunz
Super-Moderator
 
Registriert seit: 22.03.2000
Beiträge: 9.666


spunz eine Nachricht über ICQ schicken
Standard kleines perl script umbauen

ich hab hier ein perl script vom nagios projekt. damit wird der freie speicher eines shares überwacht.

die ausgabe schaut in etwa so aus:

Code:
Domain=[Work] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager]
Disk ok - 2.92G (73%) free on \\192.168.0.1\hdiskc
nun möchte ich schöne grafiken über den verbrauch des freien speichers diverser shares in cacti einrichten, dazu benötige ich aber einen reinen zahlenwert. es muß nicht unbedingt dieses perlscript sein, eine lösung als shellscript/php würde es auch tun.



hier das perl script, ich habe es auf 2 threads aufteilen müssen:

Code:
#! /usr/bin/perl 
#-wT
#
#
# check_disk.pl <host> <share> <user> <pass> [warn] [critical] [port]
#
# NetSaint host script to get the disk usage from a SMB share
#
# Changes and Modifications
# =========================
# 7-Aug-1999 - Michael Anthon
#  Created from check_disk.pl script provided with netsaint_statd (basically
#  cause I was too lazy (or is that smart?) to write it from scratch)
# 8-Aug-1999 - Michael Anthon
#  Modified [warn] and [critical] parameters to accept format of nnn[M|G] to
#  allow setting of limits in MBytes or GBytes.  Percentage settings for large
#  drives is a pain in the butt

BEGIN {
	if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
		$runtimedir = $1;
		$PROGNAME = $2;
	}
}

require 5.004;
use POSIX;
use strict;
use Getopt::Long;
use vars qw($opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
use vars qw($PROGNAME);
use lib $main::runtimedir;
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);

sub print_help ();
sub print_usage ();
sub help ();
sub version ();

delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

Getopt::Long::Configure('bundling', 'no_ignore_case');
GetOptions
	("V|version"     => \&version,
	 "h|help"        => \&help,
   "v|verbose"     => \$verbose,
	 "w|warning=s"   => \$opt_w,
	 "c|critical=s"  => \$opt_c,
	 "p|password=s"  => \$opt_p,
	 "u|username=s"  => \$opt_u,
	 "s|share=s"     => \$opt_s,
	 "W|workgroup=s" => \$opt_W,
	 "H|hostname=s"  => \$opt_H);

my $smbclient="smbclient";
my $smbclientoptions="";

($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
my $host = $1 if ($opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]*(\.[a-zA-Z][-a-zA-Z0-9]*)*)$/);
($host) || usage("Invalid host: $opt_H\n");

($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
my $share = $1 if ($opt_s =~ /([-_.A-Za-z0-9]+)/);
($share) || usage("Invalid share: $opt_s\n");

($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
my $user = $1 if ($opt_u =~ /([-_.A-Za-z0-9]+)/);
($user) || usage("Invalid user: $opt_u\n");

($opt_p) || ($opt_p = shift) || ($opt_p = "guest");
my $pass = $1 if ($opt_p =~ /(.*)/);

($opt_w) || ($opt_w = shift) || ($opt_w = 85);
my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])+/);
($warn) || usage("Invalid warning threshold: $opt_w\n");

($opt_c) || ($opt_c = shift) || ($opt_c = 95);
my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])/);
($crit) || usage("Invalid critical threshold: $opt_c\n");

my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);

my $state = "OK";
my $answer = undef;
my $res = undef;
my @lines = undef;

# Just in case of problems, let's not hang NetSaint
$SIG{'ALRM'} = sub { 
	print "No Answer from Client\n";
	exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
spunz ist offline   Mit Zitat antworten
Alt 09.03.2004, 09:19   #2
spunz
Super-Moderator
 
Registriert seit: 22.03.2000
Beiträge: 9.666


spunz eine Nachricht über ICQ schicken
Standard

Code:
# Execute an "ls" on the share using smbclient program
# get the results into $res
if (defined($workgroup)) {
	$res = qx/$smbclient \/\/$host\/$share $pass -W $workgroup -U $user $smbclientoptions -c ls/;
} else {
	$res = qx/$smbclient \/\/$host\/$share $pass -U $user $smbclientoptions -c ls/;
}
#Turn off alarm
alarm(0);

#Split $res into an array of lines
@lines = split /\n/, $res;

#Get the last line into $_
$_ = $lines[$#lines];
#print "$_\n";

#Process the last line to get free space.  
#If line does not match required regexp, return an UNKNOWN error
if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {

	my ($avail) = ($3*$2)/1024;
	my ($avail_bytes) = $avail;
	my ($capper) = int(($3/$1)*100);
	my ($mountpt) = "\\\\$host\\$share";

	#Check $warn and $crit for type (%/M/G) and set up for tests
	#P = Percent, K = KBytes
	my $warn_type;
	my $crit_type;
	if ($warn =~ /^([0-9]+$)/) {
		$warn_type = "P";
	} elsif ($warn =~ /^([0-9]+)k$/) {
		my ($warn_type) = "K";
		$warn = $1;
	} elsif ($warn =~ /^([0-9]+)M$/) {
		$warn_type = "K";
		$warn = $1 * 1024;
	} elsif ($warn =~ /^([0-9]+)G$/) {
		$warn_type = "K";
		$warn = $1 * 1048576;
	}
	if ($crit =~ /^([0-9]+$)/) {
		$crit_type = "P";
	} elsif ($crit =~ /^([0-9]+)k$/) {
		$crit_type = "K";
		$crit = $1;
	} elsif ($crit =~ /^([0-9]+)M$/) {
		$crit_type = "K";
		$crit = $1 * 1024;
	} elsif ($crit =~ /^([0-9]+)G$/) {
		$crit_type = "K";
		$crit = $1 * 1048576;
	}

	if (int($avail / 1024) > 0) {
		$avail = int($avail / 1024);
		if (int($avail /1024) > 0) {
			$avail = (int(($avail / 1024)*100))/100;
			$avail = $avail."G";
		} else {
			$avail = $avail."M";
		}
	} else {
		$avail = $avail."K";
	}

#print ":$warn:$warn_type:\n";
#print ":$crit:$crit_type:\n";
#print ":$avail:$avail_bytes:$capper:$mountpt:\n";
	if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) { 
		$answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
	} elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
		$state = "WARNING";
		$answer = "Only $avail ($capper%) free on $mountpt\n";
	} else {
		$state = "CRITICAL";
		$answer = "Only $avail ($capper%) free on $mountpt\n";
	}
} else {
	$answer = "Result from smbclient not suitable\n";
	$state = "UNKNOWN";
	foreach (@lines) {
		if (/Access denied/) {
			$answer = "Access Denied\n";
			$state = "CRITICAL";
			last;
		}
		if (/(Unknown host \w*)/) {
			$answer = "$1\n";
			$state = "CRITICAL";
			last;
		}
		if (/(You specified an invalid share name)/) {
			$answer = "Invalid share name \\\\$host\\$share\n";
			$state = "CRITICAL";
			last;
		}
	}
}


print $answer;
print "$state\n" if ($verbose);
exit $ERRORS{$state};

sub print_usage () {
	print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password> 
      -w <warn> -c <crit> [-W <workgroup>]\n";
}

sub print_help () {
	print_revision($PROGNAME,'$Revision: 1.5.2.1 $ ');
	print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop

Perl Check SMB Disk plugin for NetSaint

";
	print_usage();
	print "
-H, --hostname=HOST
   NetBIOS name of the server
-s, --share=STRING
   Share name to be tested
-W, --workgroup=STRING
   Workgroup or Domain used (Defaults to \"WORKGROUP\")
-u, --user=STRING
   Username to log in to server. (Defaults to \"guest\")
-p, --password=STRING
   Password to log in to server. (Defaults to \"guest\")
-w, --warning=INTEGER
   Percent of used space at which a warning will be generated (Default: 85%)
   
-c, --critical=INTEGER
   Percent of used space at which a critical will be generated (Defaults: 95%)
   

";
	support();
}

sub version () {
	print_revision($PROGNAME,'$Revision: 1.5.2.1 $ ');
	exit $ERRORS{'OK'};
}

sub help () {
	print_help();
	exit $ERRORS{'OK'};
}
[/code]
spunz ist offline   Mit Zitat antworten
Alt 09.03.2004, 13:33   #3
Philipp
verXENt
 
Benutzerbild von Philipp
 
Registriert seit: 01.08.1999
Beiträge: 7.084

Mein Computer

Standard

$avail_bytes dürfte den Wert in Bytes enthalten

Probiere einmal:
$answer = "Disk ok - $avail ($avail_bytes bytes / $capper%) free on $mountpt\n";
Philipp ist offline   Mit Zitat antworten
Alt 09.03.2004, 16:00   #4
spunz
Super-Moderator
 
Registriert seit: 22.03.2000
Beiträge: 9.666


spunz eine Nachricht über ICQ schicken
Standard

schaut ganz gut aus, aber das ganze scirpt ist gar etwas fett. mir würde ein kleines script reichen was einfach smbclient startet, ls macht und den freien speicher richtig berechnet.


nach nem kurzen awk crashkurs, hab ich mir folgendes gebastelt:

Code:
#!/bin/bash

smbclient \\\\192.168.1.1\\c$ -U admin passwd  -c ls | grep "blocks available" >data_smb
awk '{print $5*$6}' data_smb >data
DATA=$(cat data)
echo $DATA
spunz ist offline   Mit Zitat antworten
Antwort


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 00:41 Uhr.


Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Forum SEO by Zoints
© 2009 FSL Verlag