Skip to content

Instantly share code, notes, and snippets.

@titpetric
Last active March 3, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titpetric/ad1aa02a22cdf9c917cbbf96251a58a8 to your computer and use it in GitHub Desktop.
Save titpetric/ad1aa02a22cdf9c917cbbf96251a58a8 to your computer and use it in GitHub Desktop.
Fetch Wowza serverinfo statistics and write out SQL queries
{
"require": {
"guzzlehttp/guzzle": "~6.0"
}
}
#!/usr/bin/env php
<?php
chdir(__DIR__);
$required_extensions = array("curl", "simplexml");
foreach ($required_extensions as $required_extension) {
if (!extension_loaded($required_extension)) {
echo "Sorry, install " . $required_extension . " first.\n";
exit(255);
}
}
error_reporting(E_ALL^E_NOTICE);
include("vendor/autoload.php");
// Settings
$hosts = array_slice($argv, 1);
$username = "stats";
$password = "stats";
// Fetch data
$stats = array();
$client = new GuzzleHttp\Client();
$stats_per_vhost = array();
$stats_per_app = array();
foreach ($hosts as $host) {
$res = $client->request('GET', rtrim($host, "/") . '/serverinfo', [
'auth' => [$username, $password, 'digest']
]);
$body = (string)$res->getBody();
$serverinfo = json_decode(json_encode(simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$stats = array();
$default = array(
"Hostname" => $host,
);
$keys = array(
"TimeRunning",
"ConnectionsLimit",
"ConnectionsCurrent",
"ConnectionsTotal",
"ConnectionsTotalAccepted",
"ConnectionsTotalRejected",
);
// per-vhost statistics
if (!array_key_exists(0, $serverinfo['VHost'])) {
$serverinfo['VHost'] = array($serverinfo['VHost']);
}
// per-app statistics
foreach ($serverinfo['VHost'] as $idx => $vhost) {
if (!array_key_exists(0, $vhost['Application'])) {
$vhost['Application'] = array($vhost['Application']);
}
foreach ($vhost['Application'] as $app) {
$data = array_merge($default, array(
"VHost" => $vhost['Name'],
"Status" => $vhost['Status'],
));
foreach ($keys as $key) {
$data[$key] = $vhost[$key];
}
$stats_per_app[] = $data;
}
// per-vhost statistics
$data = array_merge($default, array(
"VHost" => $vhost['Name'],
));
foreach ($keys as $key) {
$data[$key] = $vhost[$key];
}
$stats_per_vhost[] = $data;
}
}
// print SQL
function insert($table, $columns) {
$fields = array();
foreach ($columns as $key => $val) {
$fields[] = sprintf("%s='%s'", $key, addslashes($val));
}
$query = sprintf("INSERT INTO %s SET %s, stamp=NOW();", $table, implode(", ", $fields));
echo $query . "\n";
}
echo '-- per host statistics'."\n\n";
foreach ($stats_per_vhost as $stats) {
insert("stats_per_vhost", $stats);
}
echo "\n";
echo '-- per app statistics'."\n\n";
foreach ($stats_per_app as $stats) {
insert("stats_per_app", $stats);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment