2012-06-22 17:34:23 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Name: StatsD Interface
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* License: GPLv2
|
|
|
|
* Visibility: admin
|
|
|
|
* Description: Sends Shimmie stats to a StatsD server
|
|
|
|
* Documentation:
|
|
|
|
* define('STATSD_HOST', 'my.server.com:8125'); in shimmie.conf.php to set the host
|
|
|
|
*/
|
|
|
|
|
|
|
|
_d("STATSD_HOST", null);
|
|
|
|
|
|
|
|
function dstat($name, $val) {
|
|
|
|
StatsDInterface::$stats["shimmie.$name"] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
class StatsDInterface extends Extension {
|
|
|
|
public static $stats = array();
|
|
|
|
|
2019-05-28 16:31:20 +00:00
|
|
|
private function _stats(string $type) {
|
2015-09-12 10:43:28 +00:00
|
|
|
global $_shm_event_count, $database, $_shm_load_start;
|
2015-08-02 19:39:41 +00:00
|
|
|
$time = microtime(true) - $_shm_load_start;
|
2012-06-22 17:34:23 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.hits"] = "1|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms";
|
2014-11-26 13:09:22 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.time-db"] = "{$database->dbtime}|ms";
|
2012-06-22 17:34:23 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.memory"] = memory_get_peak_usage(true)."|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.files"] = count(get_included_files())."|c";
|
2015-08-02 20:31:55 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.queries"] = $database->query_count."|c";
|
2015-08-02 19:39:41 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.events"] = $_shm_event_count."|c";
|
2012-06-22 17:34:23 +00:00
|
|
|
StatsDInterface::$stats["shimmie.$type.cache-hits"] = $database->cache->get_hits()."|c";
|
|
|
|
StatsDInterface::$stats["shimmie.$type.cache-misses"] = $database->cache->get_misses()."|c";
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:41:40 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2012-06-22 17:34:23 +00:00
|
|
|
$this->_stats("overall");
|
|
|
|
|
2013-07-05 21:22:39 +00:00
|
|
|
if($event->page_matches("post/view")) { # 40%
|
|
|
|
$this->_stats("post-view");
|
|
|
|
}
|
|
|
|
else if($event->page_matches("post/list")) { # 30%
|
2012-06-22 17:34:23 +00:00
|
|
|
$this->_stats("post-list");
|
|
|
|
}
|
2013-07-05 21:22:39 +00:00
|
|
|
else if($event->page_matches("user")) {
|
|
|
|
$this->_stats("user");
|
|
|
|
}
|
|
|
|
else if($event->page_matches("upload")) {
|
|
|
|
$this->_stats("upload");
|
|
|
|
}
|
|
|
|
else if($event->page_matches("rss")) {
|
|
|
|
$this->_stats("rss");
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
2016-06-17 21:48:28 +00:00
|
|
|
else if($event->page_matches("api")) {
|
|
|
|
$this->_stats("api");
|
|
|
|
}
|
2012-06-22 17:34:23 +00:00
|
|
|
else {
|
2015-08-02 19:39:41 +00:00
|
|
|
#global $_shm_load_start;
|
|
|
|
#$time = microtime(true) - $_shm_load_start;
|
2013-07-05 21:22:39 +00:00
|
|
|
#file_put_contents("data/other.log", "{$_SERVER['REQUEST_URI']} $time\n", FILE_APPEND);
|
2012-06-22 17:34:23 +00:00
|
|
|
$this->_stats("other");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->send(StatsDInterface::$stats, 1.0);
|
|
|
|
StatsDInterface::$stats = array();
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:41:40 +00:00
|
|
|
public function onUserCreation(UserCreationEvent $event) {
|
2019-02-22 21:26:42 +00:00
|
|
|
StatsDInterface::$stats["shimmie_events.user_creations"] = "1|c";
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 16:41:40 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event) {
|
2019-02-22 21:26:42 +00:00
|
|
|
StatsDInterface::$stats["shimmie_events.uploads"] = "1|c";
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 16:41:40 +00:00
|
|
|
public function onCommentPosting(CommentPostingEvent $event) {
|
2019-02-22 21:26:42 +00:00
|
|
|
StatsDInterface::$stats["shimmie_events.comments"] = "1|c";
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 16:41:40 +00:00
|
|
|
public function onImageInfoSet(ImageInfoSetEvent $event) {
|
2019-02-22 21:26:42 +00:00
|
|
|
StatsDInterface::$stats["shimmie_events.info-sets"] = "1|c";
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_priority(): int {return 99;}
|
2012-06-22 17:34:23 +00:00
|
|
|
|
2019-05-28 16:31:20 +00:00
|
|
|
private function send(array $data, int $sampleRate=1) {
|
2012-06-22 17:34:23 +00:00
|
|
|
if (!STATSD_HOST) { return; }
|
|
|
|
|
|
|
|
// sampling
|
|
|
|
$sampledData = array();
|
|
|
|
|
|
|
|
if ($sampleRate < 1) {
|
|
|
|
foreach ($data as $stat => $value) {
|
|
|
|
if ((mt_rand() / mt_getrandmax()) <= $sampleRate) {
|
|
|
|
$sampledData[$stat] = "$value|@$sampleRate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sampledData = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($sampledData)) { return; }
|
|
|
|
|
|
|
|
// Wrap this in a try/catch - failures in any of this should be silently ignored
|
|
|
|
try {
|
2014-04-19 05:18:49 +00:00
|
|
|
$parts = explode(":", STATSD_HOST);
|
2012-06-22 17:34:23 +00:00
|
|
|
$host = $parts[0];
|
|
|
|
$port = $parts[1];
|
|
|
|
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
|
|
|
|
if (! $fp) { return; }
|
|
|
|
foreach ($sampledData as $stat => $value) {
|
|
|
|
fwrite($fp, "$stat:$value");
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
} catch (Exception $e) {
|
2014-04-19 05:18:49 +00:00
|
|
|
// ignore any failures.
|
2012-06-22 17:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|