#!/usr/bin/php5 -q ] [-c ] [-l ] [-h]\n"; echo "\nOptions:\n"; echo "\n-h | --help\n"; echo "\t\tPrint help screen"; echo "\n-H\n"; echo "\t\tMySql hostname"; echo "\n-u\n"; echo "\t\tMySql user"; echo "\n-p\n"; echo "\t\tMySql pass"; echo "\n-d\n"; echo "\t\tMySql db-name (default: ocsweb)"; echo "\n-q\n"; echo "\t\tName of the software ( Wildcard: % ) "; echo "\n-t\n"; echo "\t\tVersion of the software\n"; echo "\t\tYou also can use \"*\" for all versions, by using double quotes. (default)"; echo "\n-w\n"; echo "\t\tWarning"; echo "\n-c\n"; echo "\t\tCritical"; echo "\n-l [item]\n"; echo "\t\tList a specific somethink\n"; echo "\t\tThe option \"-l\" without any item, provides a list of all posible items\n\n"; exit(0); } //Options $options = getopt("H:u:p:d:q:t:h:w:c:l:"); if($argc == "1" || $argv['1'] == "--help" ){ help(); } for ($i = 1; $i < $_SERVER["argc"]; $i++) { switch($_SERVER["argv"][$i]) { case "-H": $mysqlhost = $options['H']; break; case "-u": $mysqluser = $options['u']; break; case "-p": $mysqlpass = $options['p']; break; case "-d": $mysqldb = $options['d']; break; case "-q": $productname = $options['q']; case "-t": if($options['t'] != ""){ $productversion = $options['t']; } else{ $productversion = "*"; } break; case "-w": $warning = $options['w']; break; case "-c": $critical = $options['c']; break; case "-h": help(); break; case "-l": if($options['l'] == ""){ $list_tables = "yes"; } else{ $table = $options['l']; } break; } } // defaults if(!isset($mysqldb)){ $mysqldb = "ocsweb"; } // Check thresholds if($critical != "" && $warning != ""){ if($critical <= $warning){ echo "Please check the thresholds\n"; exit(1); } } // check other requirements if(!isset($mysqlhost) || ! isset($mysqluser) || ! isset($mysqlpass)){ echo "Please check the Database informations.\n"; exit(1); } if($productname == "" && $list_tables != "yes" && $table == ""){ echo "Please provide the productname which you like to check\n"; exit(1); } // mysql connect & select $link=mysql_connect($mysqlhost,$mysqluser,$mysqlpass) or die("Connection failed, missing mysql-authentification info?\n"); mysql_select_db($mysqldb, $link) or die("Could not choose database, may db-information is missing?\n"); if($list_tables == "yes"){ $query = "SHOW TABLES FROM $mysqldb"; $result = mysql_query($query); if (!$result) { echo "DB Error, could not list tables\n"; echo 'MySQL Error: ' . mysql_error(); exit(1); } while ($row = mysql_fetch_row($result)) { echo "=> {$row[0]}\n"; } mysql_free_result($result); exit(0); } else if($table != ""){ $query = "SELECT * FROM ".$table.""; $result = mysql_query($query); if (!$result) { echo "DB Error, could not list tables\n"; echo 'MySQL Error: ' . mysql_error(); exit(1); } while ($row = mysql_fetch_array($result)){ foreach($row as $item){ echo $item . "\t"; } echo "\n"; } exit(1); } // start action with option list` if($productversion == "*"){ $query = "SELECT * FROM softwares where NAME LIKE '$productname'"; $result = mysql_query($query) or die('Query failed: '. mysql_error()); } else{ $query = "SELECT * FROM softwares where NAME='$productname' AND VERSION='$productversion'"; $result = mysql_query($query) or die('Query failed: '. mysql_error()); } $count_installations = mysql_num_rows($result); // remove wildcard from productname $productname = preg_replace("/\%/", "", $productname); if($list_tables != "yes" || $table != ""){ if($critical != "" && $warning != ""){ if($count_installations >= $critical){ echo "CRITICAL: Now are $count_installations installations for $productname" ."|". $productname."=".$count_installations.";".$warning.";".$critical.";0;\n"; exit(2); }elseif($count_installations >= $warning){ echo "WARNING: Now are $count_installations installations for $productname" ."|". $productname."=".$count_installations.";".$warning.";".$critical.";0;\n"; exit(1); }else{ echo "OK: Now are $count_installations installations for $productname" ."|". $productname."=".$count_installations.";".$warning.";".$critical.";0;\n"; exit(0); } } else{ echo "OK: Now are $count_installations installations for $productname\n"; exit(0); } echo "\n".$name['NAME']." wurde ".$count_installations." mal installiert\n"; while($name = mysql_fetch_assoc($result)){ echo $name['NAME'] . "\t" . $name['PUBLISHER']."\t". $name['HARDWARE_ID']."\n"; } } ?>