Thread Nagios Plugin: check_mozybackup (3 answers)
Opened by hund at 2010-08-26 10:47

hund
 2010-08-26 10:47
#140954 #140954
User since
2010-08-24
13 Artikel
BenutzerIn
[default_avatar]
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#checkt ob der richtige datentyp ausgewählt worden ist
sub checkSizeType {     #falls $type nicht MB oder mb oder GB oder gb ist dan  bringe eine fehler meldung
        if ( $type ne 'MB' ) {
                if ( $type ne 'mb' ) {
                        if ( $type ne 'GB' ) {
                                if ( $type ne 'gb' ) {
                                        $np->nagios_die('size can only mb,Mb,gb,GB');
                                }
                        }
                }
        }
}

#erstellen einer datenbank
sub createDatabase($$$$$$) {
        my ( $host, $dbUser, $dbPassword, $dbPort, $dbHost, $databaseName ) = @_;
        my ( $db, $sql );
        if ( defined $exist ) { #wenn sqlite datnenbank exestiert führt er das nicht aus
                $dbConnection = "DBI:mysql:$databaseName;host=$dbHost;$dbPort";
                $db           =
                  DBI->connect( "$dbConnection", "$dbUser", "$dbPassword",
                        { PrintError => 0, AutoCommit => 1, } )
                  || ( $np->nagios_die('error fail login') );
                $sql = "CREATE TABLE `mozycheck`.`last_backup` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Last_date` INT NOT NULL ,
`Host` VARCHAR( 100 ) NOT NULL ,
`Timestamp` TIMESTAMP NOT NULL ,
PRIMARY KEY ( `ID` )
) ENGINE = MYISAM ;";

                my $erg = $db->prepare($sql);
                $erg->execute();
                $erg->finish();
        }
}

#verbinden mit der sqlite datei
sub checkSqlite { #überprüft ob status.dat exestirt
        unless ( -e "$dbName" ) {
                $np->nagios_die('data not exist');
        }
}

#daten-bank abfrage vom letzten backup letzten backup 
sub checkLastBackup($) {
        my ($dsn) = @_;
        my ($sth);
        $dbh = DBI->connect($dsn);
        $sth = $dbh->prepare("SELECT start_date FROM history order by start_date DESC LIMIT 1");
        $sth->execute()|| ( $np->nagios_die('error selecting last checked entry in db') );
        $startDate = $sth->fetchrow_array();

        return $startDate;
}

#überprüft ob es über den host bereits ein eintrag gibt
sub checkHost($$$$$$) {

        my ( $host, $dbUser, $dbPassword, $dbPort, $dbHost, $databaseName ) = @_;
        my ( $erg,  $db,     $sql );

        if ( defined $exist ) {
                $dbConnection = "DBI:mysql:$databaseName;host=$dbHost;$dbPort";
                $db           = DBI->connect( "$dbConnection", "$dbUser", "$dbPassword",{ PrintError => 0, AutoCommit => 1, } );
                $sql = "SELECT Host FROM `last_backup` WHERE Host = (?) ";
                $erg = $db->prepare($sql);
                $erg->execute($host)
                  || $np->nagios_die('error selecting last checked entry in db');
                $lastHost = $erg->fetchrow_array();
                
        }
        else {
                #sqlite 
                my $dbh = DBI->connect("dbi:SQLite:dbname=lastcheck.db");

                $sth = $dbh->prepare("SELECT Host FROM lastcheck WHERE Host = (?)");
                $sth->execute($host) || ( $np->nagios_die('sqlite fail') );
                $lastHost = $sth->fetchrow_array();
                
        }
return $lastHost;
}

#update oder ein insert? falls datensatz exestiert wird ein update gemacht
sub entryDatabase($$$$$$$) {
        my ( $host, $dbUser, $dbPassword, $dbPort, $dbHost, $databaseName,
                $lastHost )
          = @_;
        my ( $sql, $erg );
        if ( defined $exist ) {
                if ( $lastHost eq $host ) {
        #update 
                        $sql = "UPDATE `mozycheck`.`last_backup`SET  
`Last_date` =  '$startDate',
`Host` = '$host' WHERE Host = '$host';";
                }#insert
                else {
                        $sql = "INSERT INTO `mozycheck`.`last_backup` (
`ID` ,
`Last_date` ,
`Host` ,
`Timestamp`
)
VALUES (
NULL , '$startDate', '$host',
CURRENT_TIMESTAMP)";
                }
                #sqlite 
                $dbConnection = "DBI:mysql:$databaseName;host=$dbHost;$dbPort";
                $db           =
                  DBI->connect( "$dbConnection", "$dbUser", "$dbPassword",
                        { PrintError => 0, AutoCommit => 1, } );
                $erg = $db->prepare($sql);
                $erg->execute() || ( $np->nagios_die('error insert or update fail') );
        }
        else {
                my $dbh = DBI->connect("dbi:SQLite:dbname=lastcheck.db");
                #update
                if ( $lastHost eq $host ) {
                        $sth = $dbh->prepare("UPDATE lastcheck SET Last_check ='$startDate',Host = '$host' WHERE Host = '$host' ");
                }#insert
                else {
                        $sth =$dbh->prepare("INSERT INTO lastcheck ('Last_check','Host') VALUES ('$startDate','$host')");
                }
                $sth->execute() || ( $np->nagios_die('sqlite fail') );
        }
}

#liest denn letzten bekannten host aus 
sub lastCheck($) {
        my ($host) = @_;
        my ( $sql, $query );
        if ( defined $exist ) {
        $sql   = "SELECT Last_date FROM `last_backup` WHERE Host = (?)";
        $query = $db->prepare($sql);
        $query->execute($host)
          || ( $np->nagios_die('error selecting last checked entry in db') );
        $lastCheck = $query->fetchrow_array();
        return $lastCheck;      
}else{
        #sqlite 
                my $dbh = DBI->connect("dbi:SQLite:dbname=lastcheck.db");

                $sth = $dbh->prepare("SELECT Last_check FROM lastcheck WHERE Host = (?)");
                $sth->execute($host) || ( $np->nagios_die('sqlite fail') );
                $lastCheck = $sth->fetchrow_array();
                return $lastCheck;
}
}
#daten-bank abfrage und bearbeitet ausgabe
sub checkBackup($$) {
        my $sth;
        $sth = $dbh->prepare("SELECT backup_id,start_date,end_date,result,files,size FROM history WHERE start_date >= (?)  order by start_date");
        ( $sth->execute( $lastCheck, ) )
          || ( $np->nagios_die('error selecting last checked entry in db') );

        while ( ( $backupID, $startDate, $endDate, $result, $files, $size ) =
                $sth->fetchrow_array() )

        {

                #size in gb umwandeln
                if ( $type eq 'gb' ) {
                        $size = ( $size / $GB );
                        $size = sprintf( "%.2f", $size );#rundet size auf 2 stellen auf
                }
                if ( $type eq 'GB' ) {
                        $size = ( $size / $GB );
                        $size = sprintf( "%.2f", $size );#rundet size auf 2 stellen auf
                }
                if ( $type eq 'MB' ) {
                        $size = ( $size / $MB );
                        $size = sprintf( "%.2f", $size );#rundet size auf 2 stellen auf
                }
                if ( $type eq $MB ) {
                        $size = ( $size / $MB );
                        $size = sprintf( "%.2f", $size );#rundet size auf 2 stellen auf
                }

                #zeit des letzten backups
                $timeStamp = time();    #ermittel aktuelle zeit in unixtime
                $dateDif   = $timeStamp - $startDate;   #ermittel differenz von dem letzten datenbank eintrag bis heute
                $dateDif   = ( $dateDif / 86400 );
                $dateDif   = sprintf( "%.0f", $dateDif );

                # Perfdata methods gibt informationen mit wie zb größe und anzahl der files
                $np->add_perfdata(      
                        label => "size",
                        value => $size,
                        uom   => "/$type",
                );
                $np->add_perfdata(
                        label => "files",
                        value => $files,
                        uom   => "",
                );

                $sth->finish;
                @redif = ( $dateDif, $result );
                return @redif;
        }
}


esskar: seh kein grund für nen leeren start-thread, also umkopiert!
Last edited: 2010-08-31 09:57:35 +0200 (CEST)

View full thread Nagios Plugin: check_mozybackup