#!/usr/bin/perl package OpMon; use strict; use DBI; use POSIX; use Filesys::SmbClient; use Filesys::SmbClientParser; use vars qw(@ISA @EXPORT); require Exporter; BEGIN { our $OK = 0; our $WARNING = 1; our $CRITICAL = 2; our $UNKNOWN = 3; our $DBCONFIGFILE = "/usr/local/opmon/etc/db.php"; @ISA = qw( Exporter AutoLoader ); @EXPORT = qw( $OK $WARNING $CRITICAL $UNKNOWN $DBCONFIGFILE opmon_parse_db_config opmon_do_query opmon_test_smb_share opmon_copy_file_to_smb ); } END { } sub opmon_parse_db_config { my $o = shift; our $DBCONFIGFILE; our $OK; our $CRITICAL; open(FILE,$DBCONFIGFILE); foreach() { chomp(); if (m/^.+\$(.+)="(.*)";$/) { $$o{$1} = $2; } } close(FILE); return $OK; } sub opmon_do_query { my $db = shift; my $q = shift; my $dbh; my $sth; $dbh = DBI->connect( "DBI:mysql:".$$db{'DBNAME'}.";host=".$$db{'DBHOST'}, $$db{'DBUSER'}, $$db{'DBPASS'}, { RaiseError => 1} ); $sth = $dbh->prepare($q); $sth->execute(); return \$sth; } sub opmon_test_smb_share { my $smbinf = shift; my $fd; my $smb; our $OK; our $CRITICAL; $smb = new Filesys::SmbClient( username => $$smbinf{'user'}, password => $$smbinf{'pass'}, workgroup => $$smbinf{'workgroup'} ); $fd = $smb->opendir($$smbinf{'path'}); if ($fd == 0) { return $CRITICAL; } return $OK; } sub opmon_copy_file_to_smb { my $smbinf = shift; my $file_path = shift; my $smb; my $remote_fd; my $local_fd; my $buffer; my @aux; our $CRITICAL; our $OK; if (!-f $file_path) { $! = "Unable to open file $file_path\n"; return $CRITICAL; } $smb = new Filesys::SmbClient( username => $$smbinf{'user'}, password => $$smbinf{'pass'}, workgroup => $$smbinf{'workgroup'} ); @aux = split("\/",$file_path); $remote_fd = $smb->open(">".$$smbinf{'path'}."/".$aux[$#aux], "0666"); if ($remote_fd == 0) { return $CRITICAL; } $local_fd = open(LOCALFILE,$file_path) || die return $CRITICAL; binmode LOCALFILE; while( read(LOCALFILE, $buffer, 50) ) { $smb->write($remote_fd,$buffer) || die return $CRITICAL; } $smb->close($remote_fd); close($local_fd); return $OK; } 1;