WinFuture-Forum.de: Problem Mit Script & Crontab - WinFuture-Forum.de

Zum Inhalt wechseln

Nachrichten zum Thema: Linux
Seite 1 von 1

Problem Mit Script & Crontab


#1 Mitglied ist offline   timbowrockt 

  • Gruppe: aktive Mitglieder
  • Beiträge: 115
  • Beigetreten: 08. Dezember 08
  • Reputation: 0
  • Geschlecht:Männlich

  geschrieben 13. März 2009 - 08:29

Hallo liebe Leute!
Ich habe ein kleines Problem und zwar folgendes...:

Ein VMware ESX-Server 3.5 (basierend auf Linux) soll mit einem Script seine Festplatten abfragen. Das ganze dann in eine HTML packen und per Mail verschicken. Auslesen und Auswerten klappt, in HTML File ausgeben klappt. Per Mail verschicken klappt auch. Nach Berechnung weiß das Script welche der Festplatten z.B. nur noch 10% Speicher frei haben. Diese sollen rot hinterlegt werden in der HTML-Tabelle. Klappt auch. Die Email wird nur verschickt wenn einer dieser Festplatten den Status "Rot" erreicht. Führe ich das Schript in der Bash aus ( . /root/scripts/vdfhtml1.sh ) bekomme ich nach 2 Sek. eine Mail. Abfraogen muss ich aber jede Stunde... also ab in crontab! Hier kommt das Problem... Ich bekommen einfach keine Mails... Hier der Crontab-Eintrag und die Scripts:



Script zur Abfrage und HTML Erstellung ruft am Ende das MailScript auf...

CODE
#/bin/bash.sh
# Script for checking vmfs diskspace
cd /root/scripts/
LOG="/var/log/vdf.html"
MYDATE=$(date +%d-%m-%y)
VMFSYELLOW=80
VMFSRED=90
CHECKR=1
cat /dev/null > "${LOG}"
echo "<html>" >> "${LOG}"
echo "<head>" >> "${LOG}"
echo "<style>" >> "${LOG}"
echo "body {margin: 10px; width: 90%; font-family:verdana; font-size: 12px;}" >> "${LOG}"
echo "div#footer {font-size: 9px;}" >> "${LOG}"
echo "</style>" >> "${LOG}"
echo "</head>" >> "${LOG}"
echo "<body>" >> "${LOG}"
echo "<center>" >> "${LOG}"


echo "<table width="80%" cellspacing="8" cellpadding="5" border="2"; style=' background-color: grey; color: black'>" >> "${LOG}"
echo "<tr><th colspan='5'; align='left'; style='background-color: rgb(179,179,179); color: black'>" >> "${LOG}"
echo "VMFS genutzter Speicherplatz - srvesx02 - $MYDATE</th></tr>" >> "${LOG}"
echo "" >> "${LOG}"
echo "<tr>" >> "${LOG}"
echo "<td><b>VMFS Festplatte</b></td><td><b><p align="right">Größe</p></b></td><td><b><p align="right">Belegt</p></b></td><td><b><p
align="right">Verfügbar</p></b></td><td><b><p align="right">in %</p></b></td>" >> "${LOG}"
echo "</tr>" >> "${LOG}"
cd /root/scripts/
/usr/sbin/vdf -h -P | grep -E '^/vmfs/volumes/' | awk '{ print $2 " " $3 " " $4 " " $5 " " $6 }' | while read output ; do
DISKSIZE=$(echo $output | awk '{ print $1 }' )
DISKUSED=$(echo $output | awk '{ print $2 }' )
DISKAVAILABLE=$(echo $output | awk '{ print $3 }' )
PERCENTINUSE=$(echo $output | awk '{ print $4 }' )
VOLNAME=$(echo $output | awk '{ print $5 }' )
CUTPERC=$(echo $PERCENTINUSE | cut -d'%' -f1 )
WARNING="white"
if [ $CUTPERC -ge $VMFSYELLOW ] ; then
WARNING="yellow"
fi
if [ $CUTPERC -ge $VMFSRED ] ; then

WARNING="red"
CHECK=1
fi

echo "<tr><td style='background-color: ${WARNING}'>$VOLNAME</td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'><p align="right">$DISKSIZE</p></td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'><p align="right">$DISKUSED</p></td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'><p align="right">$DISKAVAILABLE</p></td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'><p align="right">$PERCENTINUSE</p></td></tr>" >> "${LOG}"
done
echo "</table>" >> "${LOG}"
echo "<div id='footer'>Legende: Gelb = 80% - Rot = 90%</div>" >> "${LOG}"
echo "</center>" >> "${LOG}"
echo "</body>" >> "${LOG}"
echo "</html>" >> "${LOG}"

echo "Wurde ausgeführt"
echo "CHECK=$CHECK "


if [ $CHECK -eq 1 ] ; then
/root/scripts/smtp_send.pl -t [email protected] -s "Diskspace report of the VMFS volumes" -f [email protected] -m "`cat /var/log/vdf.html`" -r 192.168.*.*
fi



Hier Mailscript smtp_send.pl...


CODE
#!/usr/bin/perl -w
# ==================================================
====================
#
# Perl Source File -- Created with SAPIEN Technologies PrimalScript 3.1
#
# NAME: smtp_send.pl
#
# ORIGINAL AUTHOR: Scott Herold , RapidApp
# ORIGINAL DATE : 11/30/2004
#
# MODIFY AUTHOR: Jeremy Pries, Xcedex (jpries-at-xcedex-dot-com)
# MODIFY DATE : 4/20/2005
#
# MODIFY AUTHOR: Duncan Epping, Yellow-Bricks.com
# MODIFY DATE : 9/7/2007
# MODIFIED: Line 99 changed TEXT to TEXT/HTML
#
# PURPOSE: This is a small script that can send emails through an
# external smtp gateway. This is useful when sending log files from an
# ESX host to an administrator or group on a scheduled basis.
#
# Handles piped (|) input, command line message content and file attachments
#
# PREREQUESITES: The MIME-Lite module is required for this script
# to function.
#
# http://search.cpan.o...ib/MIME/Lite.pm
#
# This module must be placed in the following directory of your ESX host:
# (Note, the directory will need to be created and is case sensative)
#
# /usr/lib/perl5/5.6.1/MIME
#
#
# ==================================================
====================

use MIME::Lite;
use Sys::Hostname;
use Getopt::Long;

# Variables to configure
#
# From Address. By default it just uses a hostname lookup. You may change this to something else if desired.
my $fromAddress = hostname();

# Set your smarthost here
my $smartHost = "smarthost.example.com";

### Command Line Arguments
if (@ARGV < 2) {
print "\n";
print "Usage $0: [-t address] [-s subject] [-m Body] [-a path] [-f address] [-r smarthost]\n\n";
print "-Enclose any options that contains spaces in quotes\n";
print "-All options may be shortened to one character. Ex. -t instead of -toAddress\n\n";
print "Options:\n";
print " -toAddress rcpt to address (required)\n";
print " -subject subect (required)\n";
print " -messageBody body (optional)\n";
print " -attach full path to attachment (optional)\n";
print " -fromAddress mail from address (optional)\n";
print " -relay smarthost/relayhost used to deliver mail. (optional)\n\n";
print "-If no fromAddress is specified, the address $fromAddress will be used\n\n";
print "-If no relay is specified, the variable $smartHost in the script is used.\n";
print " You may wish to change the value of this variable to prevent need to specify it on the command line.\n\n";
print "As of version 0.2, script will handle message body input via a pipe. If input is received via a pipe\n";
print " and message body on command line, the command line text will precede the pipe input in the body.\n";
print "\n";
exit(1);
}

my $toAddress = '';
my $subject = '';
my $messageBody = '';
my $attach = 'none';
GetOptions ('toAddress=s' => \$toAddress,'subject=s' => \$subject, 'messageBody=s' => \$messageBody, 'attach=s' => \$attach, 'fromAddress=s' => \$fromAddress, 'relay=s' => \$smartHost);

unless (-t) {
$messageBody = $messageBody . "\n";
while ( $line = <STDIN> ) {
chomp $line;
$messageBody = $messageBody . $line . "\n";
}
}
print "smartHost: $smartHost\n";
print "to: $toAddress\n";
print "subject: $subject\n";
print "attach: $attach\n";

### Default Email header stuff
$msg = MIME::Lite->new(
From =>$fromAddress,
To =>$toAddress,
Subject =>$subject,
Type =>'multipart/mixed'
);


### The text message portion of the email. Remember \n to break lines
$msg->attach(Type =>'TEXT/HTML',
Data =>$messageBody
);

### Attach a file if necessary
if ($attach ne "none") {
### Path is full path to file, Filename is file name as attached (Can be
### different than the source file name)
$msg->attach(Type =>'applications/zip',
Path =>$attach
);
}

### Mail type and SMTP server. Send email based off those settings
MIME::Lite->send('smtp', $smartHost);
$msg->send;


und hier der Crontab-Eintrag:

CODE
*/1 * * * * root /root/scripts/vdfhtml1.sh


Hier ist jetzt für tests jede Minute eingestellt...

Vielen Dank für eure Hilfe...

Gruß
0

Anzeige



#2 Mitglied ist offline   SpaceCowboy 

  • Gruppe: aktive Mitglieder
  • Beiträge: 762
  • Beigetreten: 17. August 04
  • Reputation: 1

geschrieben 13. März 2009 - 11:18

schonmal versucht root aus dem script rauszunehmen?

*/1 * * * *  /root/scripts/vdfhtml1.sh


was sagen ausserdem die logs wenn das script läuft?

Dieser Beitrag wurde von SpaceCowboy bearbeitet: 13. März 2009 - 11:21

have you tried turning it off and on again?
0

#3 Mitglied ist offline   timbowrockt 

  • Gruppe: aktive Mitglieder
  • Beiträge: 115
  • Beigetreten: 08. Dezember 08
  • Reputation: 0
  • Geschlecht:Männlich

geschrieben 13. März 2009 - 17:00

Hi!

Danke mal für deine Hilfe...
Das Crontab logfile ist nur hiermit gefüllt...



Mar 12 19:35:01 srvesx02 crond[12264]: (root) CMD (/root/scripts/vdfhtml1.sh)
Mar 12 19:36:01 srvesx02 crond[13007]: (root) CMD (/root/scripts/vdfhtml1.sh)
Mar 12 19:37:01 srvesx02 crond[13285]: (root) CMD (/root/scripts/vdfhtml1.sh)
.
.
.
usw.

Keine Ahnung woran das liegen könnte, für mich sieht es so aus als ob der den Job normal ausführt...

Gruß
0

#4 Mitglied ist offline   deen 

  • Gruppe: aktive Mitglieder
  • Beiträge: 36
  • Beigetreten: 23. Januar 08
  • Reputation: 0

geschrieben 13. März 2009 - 17:27

Ändere mal im Shebang von "#/bin/bash.sh" auf "#!/bin/bash".

Außerdem steht cron in der Regel nicht die selbe Umgebung wie deinem Terminal
zur Verfügung. Könntest du mit einem Eintrag in die crontab überprüfen, der
env in ne Datei schreibt. Eventuell kennt der nämlich den Pfad zu awk, cut,
usw nicht. Kannst natürlich auch den absoluten Pfad angeben.
0

#5 Mitglied ist offline   SpaceCowboy 

  • Gruppe: aktive Mitglieder
  • Beiträge: 762
  • Beigetreten: 17. August 04
  • Reputation: 1

geschrieben 13. März 2009 - 17:33

Beitrag anzeigenZitat (deen: 13.03.2009, 18:27)

Ändere mal im Shebang von "#/bin/bash.sh" auf "#!/bin/bash".

Außerdem steht cron in der Regel nicht die selbe Umgebung wie deinem Terminal
zur Verfügung. Könntest du mit einem Eintrag in die crontab überprüfen, der
env in ne Datei schreibt. Eventuell kennt der nämlich den Pfad zu awk, cut,
usw nicht. Kannst natürlich auch den absoluten Pfad angeben.


stimmt, das hab ich gar nicht gesehen.
dass er den cron abarbeitet ist ersichtlich. aber die anderen logs würden mich interessieren. zB ob das script dann tasächlich raussendet usw.

ich hab ein ähnliches perl-script laufen dass mir den platz checkt und bei einer bestimmten %zahl eine mail schickt. wenn ich im cron den user angegeben habe hatte mir aber das script den falschen user zum postfix übergeben, kA wieso. deshalb war das meine erste vermutung.

Dieser Beitrag wurde von SpaceCowboy bearbeitet: 13. März 2009 - 17:33

have you tried turning it off and on again?
0

Thema verteilen:


Seite 1 von 1

1 Besucher lesen dieses Thema
Mitglieder: 0, Gäste: 1, unsichtbare Mitglieder: 0