So, da das Wiki mich ärgert, poste ich den Code jetzt hier...
Description
This Script can be used as a cronjob to make all clients do a hwaudit and swaudit on a regular basis.
The script does only work with MySQL-Backend (probably also works when using free variant with only inventory in DB!)
Hint: In my variant I am using an event "request_graceful_reboot" which issues a popup requesting the user to reboot. This event has to be defined on every client. You could also remove that singe RPC-Call below.
Usage
./opsi-audit-cron.php [-d] [-t]
* -d Enables Debug-Output for diagnostic
* -t Enables Trace-Output (e.g. returned Data from RPC-Call) - Does not imply Debug!
For Cronjobs you should use some output redirection. For example like that:
Code: Alles auswählen
./opsi-audit-cron.php >> /var/log/opsi/opsi-audit-cron.log
Code
cli.lib.php
Code: Alles auswählen
<?php
if (defined('DEBUG') && DEBUG) d('Debug-Mode ist aktiv!');
if (defined('TRACE') && TRACE) t('TRACE-Mode ist aktiv!');
function d($str) {
if (!defined('DEBUG') || !DEBUG) return false;
$ary = explode("\n", str_replace("\r", '', $str));
foreach ($ary as $line) {
print "\033[1;34m[DEBUG] ".$line."\033[0m\r\n";
}
unset ($str, $ary, $line);
}
function t($str) {
if (!defined('TRACE') || !TRACE) return false;
$ary = explode("\n", str_replace("\r", '', $str));
foreach ($ary as $line) {
print "\033[0;34m[TRACE] ".$line."\033[0m\r\n";
}
unset ($str, $ary, $line);
}
function i($str) {
$ary = explode("\n", str_replace("\r", '', $str));
foreach ($ary as $line) {
print "\033[0;32m[INFO] ".$line."\033[0m\r\n";
}
unset ($str, $ary, $line);
}
function e($str, $die=false) {
$ary = explode("\n", str_replace("\r", '', $str));
foreach ($ary as $line) {
print "\033[0;31m[ERROR] ".$line."\033[0m\r\n";
}
if ($die){
e("Exiting because of previous error...");
exit(255);
}
unset ($str, $ary, $line);
}
//borrowed from http://stackoverflow.com/questions/4929584/encodeuri-in-php/6059053
function encodeURI($uri)
{
return preg_replace_callback("{[^0-9a-z_.!~*'();,/?:@&=+$#-]}i", function ($m) {
return sprintf('%%%02X', ord($m[0]));
}, $uri);
}
?>
opsi-audit-cron.php
Code: Alles auswählen
#!/usr/bin/php
<?php
if (in_array('-d', $argv)) define('DEBUG', true);
if (in_array('-t', $argv)) define('TRACE', true);
require "cli.lib.php";
d("Connecting to DB");
$db = new mysqli('localhost', '**dbuser**', '**dbpass**', '**dbname**');
function rpc($call, $base='rpc') {
t('Calling URL: https://**172.16.10.10**:4447/'.$base.'?'.encodeURI($call));
$rpc = curl_init('https://**172.16.10.10**:4447/'.$base.'?'.encodeURI($call));
curl_setopt($rpc, CURLOPT_USERPWD, "**adminuser:password**");
curl_setopt($rpc, CURLOPT_TIMEOUT, 15);
curl_setopt($rpc, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($rpc, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($rpc, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($rpc);
$httpcode = curl_getinfo($rpc, CURLINFO_HTTP_CODE);
if (curl_errno($rpc)) {
e('Curl-Error '.curl_errno($rpc).': '.curl_error($rpc));
d(print_r(curl_getinfo($rpc), true));
return false;
}
if ($httpcode != 200) e('RPC-Api-Error '.$httpcode);
curl_close($rpc);
return $return;
}
//Healthy check
d("Checking healthy of runtime");
if (mysqli_connect_errno()) e(mysqli_connect_error(), true);
d("Checking for too many clients waiting for swaudit");
$sql = $db->prepare("SELECT COUNT(clientId) as cnt FROM PRODUCT_ON_CLIENT WHERE productId='swaudit' AND actionRequest='setup' AND modificationTime > TIMESTAMP(DATE_SUB(NOW(), INTERVAL 62 DAY))");
if ($db->error) e('MySQL-Error: '.$db->error, true);
$sql->execute();
$sql->bind_result($cnt);
$sql->fetch();
if ($cnt > 350) e("Zu viele Clients ($cnt, Limit 350) warten derzeit bereits auf Software-Inventarisierung", true);
$sql->close();
d("Querying MySQL for data");
$sql = $db->prepare("SELECT c.hostId FROM HOST c LEFT JOIN PRODUCT_ON_CLIENT p ON p.clientId = c.hostId WHERE p.productId = 'swaudit' AND p.actionRequest <> 'setup' AND p.modificationTime < TIMESTAMP(DATE_SUB(NOW(), INTERVAL 14 DAY)) order by RAND() ASC LIMIT 175");
if ($db->error) e('MySQL-Error: '.$db->error, true);
$sql->execute();
$sql->bind_result($host);
d("And today's winners are...");
$pcs='';
while ($sql->fetch()) {
set_time_limit(40); //Max. 40 Sekunden pro Client
i('['.date('d.m.Y H:i:s').'] Processing: '.$host);
t(rpc('[ { "method": "setProductActionRequest","params": ["swaudit", "'.$host.'", "setup"],"id": 1 }, { "method": "setProductActionRequest","params": ["hwaudit", "'.$host.'", "setup"],"id": 2 }, { "method": "hostControlSafe_fireEvent","params": ["request_graceful_reboot", "'.$host.'"],"id": 3 } ]'));
sleep (20);
}
$sql->close();
?>
Note: you have to change ServerIP, passwords, usernames, dbname... those values are marked with asterisks
Wiki:
wiki/doku.php?id=userspace:opsi-audit-cron