Completed
Pull Request — master (#4)
by Frédéric G.
06:18 queued 05:15
created

QueueAgePlugin::config()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @file
5
 * Munin plugin for Beanstalkd "age of waiting jobs in tubes" monitoring
6
 *
7
 * @author: Frédéric G. MARAND <[email protected]>
8
 * @copyright (c) 2014-2018 Ouest Systèmes Informatiques (OSInet)
9
 * @license Apache License 2.0 or later
10
 */
11
12
namespace OSInet\Beanstalkd\Munin;
13
14
use Pheanstalk\Exception\ServerException;
15
16
class QueueAgePlugin extends BasePlugin
17
{
18
    public $tubes = [];
19
20
    public static function cleanTube($tube)
21
    {
22
        return str_replace('.', '_', $tube);
23
    }
24
25
    public static function createFromGlobals()
26
    {
27
        /** @var \OSInet\Beanstalkd\Munin\QueueAgePlugin $instance */
28
        $instance = parent::createFromGlobals();
29
30
        $tubes = getenv('TUBES') ?: 'default';
31
        $tubesArray = explode(' ', $tubes);
32
33
        foreach ($tubesArray as $tube) {
34
            $instance->tubes[$instance->cleanTube($tube)] = $tube;
35
        }
36
37
        return $instance;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function config()
44
    {
45
        $ret = <<<'EOT'
46
graph_title Job Age
47
graph_vlabel Max Age
48
graph_category Beanstalk
49
graph_args --lower-limit 0
50
graph_scale no
51
52
EOT;
53
54
        foreach (array_keys($this->tubes) as $clean) {
55
            $ret .= sprintf("%s_jobs.label %s\n", $clean, $clean);
56
            $ret .= sprintf("%s_jobs.type GAUGE\n", $clean)
57
              . sprintf("%s_jobs.min 0\n", $clean);
58
        }
59
        return $ret;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function data()
66
    {
67
        $server = $this->server;
68
        $ret = '';
69
70
        foreach ($this->tubes as $clean => $tube) {
71
            $server->useTube($tube);
72
            try {
73
                $job = $server->peekReady();
74
            } catch (ServerException $e) {
75
                $job = null;
76
            }
77
78
            $val = isset($job)
79
              ? $server->statsJob($job)['age']
80
              : 0;
81
82
            $ret .= sprintf("%s_jobs.value %d\n", $clean, $val);
83
        }
84
85
        return $ret;
86
    }
87
}
88