Example #1
0
function ss_get_by_ssh($options)
{
    global $debug, $cache_dir, $poll_time;
    # Build and test the type-specific function names.
    $caching_func = "{$options['type']}_cachefile";
    $cmdline_func = "{$options['type']}_cmdline";
    $parsing_func = "{$options['type']}_parse";
    $getting_func = "{$options['type']}_get";
    debug("Functions: '{$caching_func}', '{$cmdline_func}', '{$parsing_func}'");
    if (!function_exists($cmdline_func)) {
        die("The parsing function '{$cmdline_func}' does not exist");
    }
    if (!function_exists($parsing_func)) {
        die("The parsing function '{$parsing_func}' does not exist");
    }
    # Check the cache.
    $fp = null;
    if (!isset($options['file']) && $cache_dir && !array_key_exists('nocache', $options) && function_exists($caching_func)) {
        $cache_file = call_user_func($caching_func, $options);
        $cache_res = check_cache($cache_dir, $poll_time, $cache_file, $options);
        if ($cache_res[1]) {
            debug("The cache is usable.");
            return extract_desired($options, $cache_res[1]);
        } elseif ($cache_res[0]) {
            $fp = $cache_res[0];
            debug("Got a filehandle to the cache file");
        }
    }
    if (!$fp) {
        debug("Caching is disabled.");
    }
    # There might be a custom function that overrides the SSH fetch.
    if (!isset($options['file']) && function_exists($getting_func)) {
        debug("{$getting_func}() is defined, will call it");
        $output = call_user_func($getting_func, $options);
    } else {
        # Get the command-line to fetch the data, then fetch and parse the data.
        debug("No getting_func(), will use normal code path");
        $cmd = call_user_func($cmdline_func, $options);
        debug($cmd);
        $output = get_command_result($cmd, $options);
    }
    debug($output);
    $result = call_user_func($parsing_func, $options, $output);
    # Define the variables to output.  I use shortened variable names so maybe
    # it'll all fit in 1024 bytes for Cactid and Spine's benefit.  However, don't
    # use things that have only hex characters, thus begin with 'gg' to avoid a
    # bug in Cacti.  This list must come right after the word
    # MAGIC_VARS_DEFINITIONS.  The Perl script parses it and uses it as a Perl
    # variable.
    $keys = array('APACHE_Requests' => 'gg', 'APACHE_Bytes_sent' => 'gh', 'APACHE_Idle_workers' => 'gi', 'APACHE_Busy_workers' => 'gj', 'APACHE_CPU_Load' => 'gk', 'APACHE_Waiting_for_connection' => 'gl', 'APACHE_Starting_up' => 'gm', 'APACHE_Reading_request' => 'gn', 'APACHE_Sending_reply' => 'go', 'APACHE_Keepalive' => 'gp', 'APACHE_DNS_lookup' => 'gq', 'APACHE_Closing_connection' => 'gr', 'APACHE_Logging' => 'gs', 'APACHE_Gracefully_finishing' => 'gt', 'APACHE_Idle_cleanup' => 'gu', 'APACHE_Open_slot' => 'gv', 'STAT_CPU_user' => 'gw', 'STAT_CPU_nice' => 'gx', 'STAT_CPU_system' => 'gy', 'STAT_CPU_idle' => 'gz', 'STAT_CPU_iowait' => 'hg', 'STAT_CPU_irq' => 'hh', 'STAT_CPU_softirq' => 'hi', 'STAT_CPU_steal' => 'hj', 'STAT_CPU_guest' => 'hk', 'STAT_interrupts' => 'hl', 'STAT_context_switches' => 'hm', 'STAT_forks' => 'hn', 'STAT_loadavg' => 'ho', 'STAT_numusers' => 'hp', 'STAT_memcached' => 'hq', 'STAT_membuffer' => 'hr', 'STAT_memshared' => 'hs', 'STAT_memfree' => 'ht', 'STAT_memused' => 'hu', 'STAT_memtotal' => 'hv', 'NGINX_active_connections' => 'hw', 'NGINX_server_accepts' => 'hx', 'NGINX_server_handled' => 'hy', 'NGINX_server_requests' => 'hz', 'NGINX_reading' => 'ig', 'NGINX_writing' => 'ih', 'NGINX_waiting' => 'ii', 'MEMC_rusage_user' => 'ij', 'MEMC_rusage_system' => 'ik', 'MEMC_curr_items' => 'il', 'MEMC_total_items' => 'im', 'MEMC_bytes' => 'in', 'MEMC_curr_connections' => 'io', 'MEMC_total_connections' => 'ip', 'MEMC_cmd_get' => 'iq', 'MEMC_cmd_set' => 'ir', 'MEMC_get_misses' => 'is', 'MEMC_evictions' => 'it', 'MEMC_bytes_read' => 'iu', 'MEMC_bytes_written' => 'iv', 'DISK_reads' => 'iw', 'DISK_reads_merged' => 'ix', 'DISK_sectors_read' => 'iy', 'DISK_time_spent_reading' => 'iz', 'DISK_writes' => 'jg', 'DISK_writes_merged' => 'jh', 'DISK_sectors_written' => 'ji', 'DISK_time_spent_writing' => 'jj', 'DISK_io_ops' => 'jk', 'DISK_io_time' => 'jl', 'DISK_io_time_weighted' => 'jm', 'OPVZ_kmemsize_held' => 'jn', 'OPVZ_kmemsize_failcnt' => 'jo', 'OPVZ_lockedpages_held' => 'jp', 'OPVZ_lockedpages_failcnt' => 'jq', 'OPVZ_privvmpages_held' => 'jr', 'OPVZ_privvmpages_failcnt' => 'js', 'OPVZ_shmpages_held' => 'jt', 'OPVZ_shmpages_failcnt' => 'ju', 'OPVZ_numproc_held' => 'jv', 'OPVZ_numproc_failcnt' => 'jw', 'OPVZ_physpages_held' => 'jx', 'OPVZ_physpages_failcnt' => 'jy', 'OPVZ_vmguarpages_held' => 'jz', 'OPVZ_vmguarpages_failcnt' => 'kg', 'OPVZ_oomguarpages_held' => 'kh', 'OPVZ_oomguarpages_failcnt' => 'ki', 'OPVZ_numtcpsock_held' => 'kj', 'OPVZ_numtcpsock_failcnt' => 'kk', 'OPVZ_numflock_held' => 'kl', 'OPVZ_numflock_failcnt' => 'km', 'OPVZ_numpty_held' => 'kn', 'OPVZ_numpty_failcnt' => 'ko', 'OPVZ_numsiginfo_held' => 'kp', 'OPVZ_numsiginfo_failcnt' => 'kq', 'OPVZ_tcpsndbuf_held' => 'kr', 'OPVZ_tcpsndbuf_failcnt' => 'ks', 'OPVZ_tcprcvbuf_held' => 'kt', 'OPVZ_tcprcvbuf_failcnt' => 'ku', 'OPVZ_othersockbuf_held' => 'kv', 'OPVZ_othersockbuf_failcnt' => 'kw', 'OPVZ_dgramrcvbuf_held' => 'kx', 'OPVZ_dgramrcvbuf_failcnt' => 'ky', 'OPVZ_numothersock_held' => 'kz', 'OPVZ_numothersock_failcnt' => 'lg', 'OPVZ_dcachesize_held' => 'lh', 'OPVZ_dcachesize_failcnt' => 'li', 'OPVZ_numfile_held' => 'lj', 'OPVZ_numfile_failcnt' => 'lk', 'OPVZ_numiptent_held' => 'll', 'OPVZ_numiptent_failcnt' => 'lm', 'REDIS_connected_clients' => 'ln', 'REDIS_connected_slaves' => 'lo', 'REDIS_used_memory' => 'lp', 'REDIS_changes_since_last_save' => 'lq', 'REDIS_total_connections_received' => 'lr', 'REDIS_total_commands_processed' => 'ls', 'JMX_heap_memory_used' => 'lt', 'JMX_heap_memory_committed' => 'lu', 'JMX_heap_memory_max' => 'lv', 'JMX_non_heap_memory_used' => 'lw', 'JMX_non_heap_memory_committed' => 'lx', 'JMX_non_heap_memory_max' => 'ly', 'JMX_open_file_descriptors' => 'lz', 'JMX_max_file_descriptors' => 'mg', 'JMX_current_threads_busy' => 'mh', 'JMX_current_thread_count' => 'mi', 'JMX_max_threads' => 'mj', 'MONGODB_connected_clients' => 'mk', 'MONGODB_used_resident_memory' => 'ml', 'MONGODB_used_mapped_memory' => 'mm', 'MONGODB_used_virtual_memory' => 'mn', 'MONGODB_index_accesses' => 'mo', 'MONGODB_index_hits' => 'mp', 'MONGODB_index_misses' => 'mq', 'MONGODB_index_resets' => 'mr', 'MONGODB_back_flushes' => 'ms', 'MONGODB_back_total_ms' => 'mt', 'MONGODB_back_average_ms' => 'mu', 'MONGODB_back_last_ms' => 'mv', 'MONGODB_op_inserts' => 'mw', 'MONGODB_op_queries' => 'mx', 'MONGODB_op_updates' => 'my', 'MONGODB_op_deletes' => 'mz', 'MONGODB_op_getmores' => 'ng', 'MONGODB_op_commands' => 'nh', 'MONGODB_slave_lag' => 'ni', 'DISKFREE_used' => 'nj', 'DISKFREE_available' => 'nk', 'NETDEV_inbound' => 'nl', 'NETDEV_rxerrs' => 'nm', 'NETDEV_rxdrop' => 'nn', 'NETDEV_rxfifo' => 'no', 'NETDEV_rxframe' => 'np', 'NETDEV_outbound' => 'nq', 'NETDEV_txerrs' => 'nr', 'NETDEV_txdrop' => 'ns', 'NETDEV_txfifo' => 'nt', 'NETDEV_txcolls' => 'nu', 'NETDEV_txcarrier' => 'nv', 'NETSTAT_established' => 'nw', 'NETSTAT_syn_sent' => 'nx', 'NETSTAT_syn_recv' => 'ny', 'NETSTAT_fin_wait1' => 'nz', 'NETSTAT_fin_wait2' => 'og', 'NETSTAT_time_wait' => 'oh', 'NETSTAT_close' => 'oi', 'NETSTAT_close_wait' => 'oj', 'NETSTAT_last_ack' => 'ok', 'NETSTAT_listen' => 'ol', 'NETSTAT_closing' => 'om', 'NETSTAT_unknown' => 'on', 'VMSTAT_pswpin' => 'oo', 'VMSTAT_pswpout' => 'op');
    # Prepare and return the output.  The output we have right now is the whole
    # info, and we need that -- to write it to the cache file -- but what we
    # return should be only the desired items.
    $output = array();
    foreach ($keys as $key => $short) {
        # If the value isn't defined, return -1 which is lower than (most graphs')
        # minimum value of 0, so it'll be regarded as a missing value.
        $val = isset($result[$key]) ? $result[$key] : -1;
        $output[] = "{$short}:{$val}";
    }
    $result = implode(' ', $output);
    if ($fp) {
        if (fwrite($fp, $result) === FALSE) {
            die("Cannot write to '{$cache_file}'");
        }
        fclose($fp);
    }
    return extract_desired($options, $result);
}
Example #2
0
function ss_get_by_ssh($options)
{
    global $debug, $cache_dir, $poll_time;
    # Build and test the type-specific function names.
    $caching_func = "{$options['type']}_cachefile";
    $cmdline_func = "{$options['type']}_cmdline";
    $parsing_func = "{$options['type']}_parse";
    $getting_func = "{$options['type']}_get";
    debug("Functions: '{$caching_func}', '{$cmdline_func}', '{$parsing_func}'");
    if (!function_exists($cmdline_func)) {
        die("The parsing function '{$cmdline_func}' does not exist");
    }
    if (!function_exists($parsing_func)) {
        die("The parsing function '{$parsing_func}' does not exist");
    }
    # Check the cache.
    $fp = null;
    if (!isset($options['file']) && $cache_dir && !isset($options['nocache']) && function_exists($caching_func)) {
        $cache_file = call_user_func($caching_func, $options);
        $cache_res = check_cache($cache_dir, $poll_time, $cache_file, $options);
        if ($cache_res[1]) {
            debug("The cache is usable.");
            return extract_desired($options, $cache_res[1]);
        } elseif ($cache_res[0]) {
            $fp = $cache_res[0];
            debug("Got a filehandle to the cache file");
        }
    }
    if (!$fp) {
        debug("Caching is disabled.");
    }
    # There might be a custom function that overrides the SSH fetch.
    if (!isset($options['file']) && function_exists($getting_func)) {
        debug("{$getting_func}() is defined, will call it");
        $output = call_user_func($getting_func, $options);
    } else {
        # Get the command-line to fetch the data, then fetch and parse the data.
        debug("No getting_func(), will use normal code path");
        $cmd = call_user_func($cmdline_func, $options);
        debug($cmd);
        $output = get_command_result($cmd, $options);
    }
    debug($output);
    $result = call_user_func($parsing_func, $options, $output);
    # Define the variables to output.  I use shortened variable names so maybe
    # it'll all fit in 1024 bytes for Cactid and Spine's benefit.  This list must
    # come right after the word MAGIC_VARS_DEFINITIONS.  The Perl script parses
    # it and uses it as a Perl variable.
    $keys = array('Requests' => 'a0', 'Bytes_sent' => 'a1', 'Idle_workers' => 'a2', 'Busy_workers' => 'a3', 'CPU_Load' => 'a4', 'Waiting_for_connection' => 'a5', 'Starting_up' => 'a6', 'Reading_request' => 'a7', 'Sending_reply' => 'a8', 'Keepalive' => 'a9', 'DNS_lookup' => 'aa', 'Closing_connection' => 'ab', 'Logging' => 'ac', 'Gracefully_finishing' => 'ad', 'Idle_cleanup' => 'ae', 'Open_slot' => 'af', 'STAT_CPU_user' => 'ag', 'STAT_CPU_nice' => 'ah', 'STAT_CPU_system' => 'ai', 'STAT_CPU_idle' => 'aj', 'STAT_CPU_iowait' => 'ak', 'STAT_CPU_irq' => 'al', 'STAT_CPU_softirq' => 'am', 'STAT_CPU_steal' => 'an', 'STAT_CPU_guest' => 'ao', 'STAT_interrupts' => 'ap', 'STAT_context_switches' => 'aq', 'STAT_forks' => 'ar', 'STAT_loadavg' => 'as', 'STAT_numusers' => 'at', 'STAT_memcached' => 'au', 'STAT_membuffer' => 'av', 'STAT_memshared' => 'aw', 'STAT_memfree' => 'ax', 'STAT_memused' => 'ay', 'NGINX_active_connections' => 'az', 'NGINX_server_accepts' => 'b0', 'NGINX_server_handled' => 'b1', 'NGINX_server_requests' => 'b2', 'NGINX_reading' => 'b3', 'NGINX_writing' => 'b4', 'NGINX_waiting' => 'b5', 'MEMC_rusage_user' => 'b6', 'MEMC_rusage_system' => 'b7', 'MEMC_curr_items' => 'b8', 'MEMC_total_items' => 'b9', 'MEMC_bytes' => 'ba', 'MEMC_curr_connections' => 'bb', 'MEMC_total_connections' => 'bc', 'MEMC_cmd_get' => 'bd', 'MEMC_cmd_set' => 'be', 'MEMC_get_misses' => 'bf', 'MEMC_evictions' => 'bg', 'MEMC_bytes_read' => 'bh', 'MEMC_bytes_written' => 'bi', 'DISK_reads' => 'bj', 'DISK_reads_merged' => 'bk', 'DISK_sectors_read' => 'bl', 'DISK_time_spent_reading' => 'bm', 'DISK_writes' => 'bn', 'DISK_writes_merged' => 'bo', 'DISK_sectors_written' => 'bp', 'DISK_time_spent_writing' => 'bq', 'DISK_io_ops_in_progress' => 'br', 'DISK_io_time' => 'bs', 'DISK_io_time_weighted' => 'bt', 'OPVZ_kmemsize_held' => 'bu', 'OPVZ_kmemsize_failcnt' => 'bv', 'OPVZ_lockedpages_held' => 'bw', 'OPVZ_lockedpages_failcnt' => 'bx', 'OPVZ_privvmpages_held' => 'by', 'OPVZ_privvmpages_failcnt' => 'bz', 'OPVZ_shmpages_held' => 'c0', 'OPVZ_shmpages_failcnt' => 'c1', 'OPVZ_numproc_held' => 'c2', 'OPVZ_numproc_failcnt' => 'c3', 'OPVZ_physpages_held' => 'c4', 'OPVZ_physpages_failcnt' => 'c5', 'OPVZ_vmguarpages_held' => 'c6', 'OPVZ_vmguarpages_failcnt' => 'c7', 'OPVZ_oomguarpages_held' => 'c8', 'OPVZ_oomguarpages_failcnt' => 'c9', 'OPVZ_numtcpsock_held' => 'ca', 'OPVZ_numtcpsock_failcnt' => 'cb', 'OPVZ_numflock_held' => 'cc', 'OPVZ_numflock_failcnt' => 'cd', 'OPVZ_numpty_held' => 'ce', 'OPVZ_numpty_failcnt' => 'cf', 'OPVZ_numsiginfo_held' => 'cg', 'OPVZ_numsiginfo_failcnt' => 'ch', 'OPVZ_tcpsndbuf_held' => 'ci', 'OPVZ_tcpsndbuf_failcnt' => 'cj', 'OPVZ_tcprcvbuf_held' => 'ck', 'OPVZ_tcprcvbuf_failcnt' => 'cl', 'OPVZ_othersockbuf_held' => 'cm', 'OPVZ_othersockbuf_failcnt' => 'cn', 'OPVZ_dgramrcvbuf_held' => 'co', 'OPVZ_dgramrcvbuf_failcnt' => 'cp', 'OPVZ_numothersock_held' => 'cq', 'OPVZ_numothersock_failcnt' => 'cr', 'OPVZ_dcachesize_held' => 'cs', 'OPVZ_dcachesize_failcnt' => 'ct', 'OPVZ_numfile_held' => 'cu', 'OPVZ_numfile_failcnt' => 'cv', 'OPVZ_numiptent_held' => 'cw', 'OPVZ_numiptent_failcnt' => 'cx', 'REDIS_connected_clients' => 'cy', 'REDIS_connected_slaves' => 'cz', 'REDIS_used_memory' => 'd0', 'REDIS_changes_since_last_save' => 'd1', 'REDIS_total_connections_received' => 'd2', 'REDIS_total_commands_processed' => 'd3', 'JMX_heap_memory_used' => 'd4', 'JMX_heap_memory_committed' => 'd5', 'JMX_heap_memory_max' => 'd6', 'JMX_non_heap_memory_used' => 'd7', 'JMX_non_heap_memory_committed' => 'd8', 'JMX_non_heap_memory_max' => 'd9', 'JMX_open_file_descriptors' => 'da', 'JMX_max_file_descriptors' => 'db', 'MONGODB_connected_clients' => 'dc', 'MONGODB_used_resident_memory' => 'dd', 'MONGODB_used_mapped_memory' => 'de', 'MONGODB_used_virtual_memory' => 'df', 'MONGODB_index_accesses' => 'dg', 'MONGODB_index_hits' => 'dh', 'MONGODB_index_misses' => 'di', 'MONGODB_index_resets' => 'dj', 'MONGODB_back_flushes' => 'dk', 'MONGODB_back_total_ms' => 'dl', 'MONGODB_back_average_ms' => 'dm', 'MONGODB_back_last_ms' => 'dn', 'MONGODB_op_inserts' => 'do', 'MONGODB_op_queries' => 'dp', 'MONGODB_op_updates' => 'dq', 'MONGODB_op_deletes' => 'dr', 'MONGODB_op_getmores' => 'ds', 'MONGODB_op_commands' => 'dt', 'MONGODB_slave_lag' => 'du');
    # Prepare and return the output.  The output we have right now is the whole
    # info, and we need that -- to write it to the cache file -- but what we
    # return should be only the desired items.
    $output = array();
    foreach ($keys as $key => $short) {
        # If the value isn't defined, return -1 which is lower than (most graphs')
        # minimum value of 0, so it'll be regarded as a missing value.
        $val = isset($result[$key]) ? $result[$key] : -1;
        $output[] = "{$short}:{$val}";
    }
    $result = implode(' ', $output);
    if ($fp) {
        if (fwrite($fp, $result) === FALSE) {
            die("Cannot write to '{$cache_file}'");
        }
        fclose($fp);
    }
    return extract_desired($options, $result);
}
<?php

require 'test-more.php';
require '../../cacti/scripts/ss_get_by_ssh.php';
$debug = true;
is(to_float('74900191315.1170664159 dollars per hour'), '74900191315.1170664159', 'to_float 74900191315.1170664159');
/* This function was used for diskstats, but then discarded

# ============================================================================
# Divide $left by $right as accurately as possible with reasonable effort.
# ============================================================================
function big_divide ($left, $right) {
   if ( function_exists("bcdiv") ) {
      debug(array('bcdiv', $left, $right, 6));
      return rtrim(bcdiv( $left, $right, 6 ), '0');
   }
   else {
      debug(array('sprintf', $left, $right));
      return rtrim(sprintf(".0f", $left / $right), '0');
   }
}

is(
   big_divide('500000', '128.193474'),
   '3900.354553',
   'big_divide 500000.0/128.193474'
);
*/
is(extract_desired(array('items' => 'a1,a2'), 'a1:45 b2:90 a3:0'), 'a1:45', 'extract_desired');