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

QueueAgePlugin::createFromGlobals()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
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
class QueueAgePlugin extends BasePlugin
15
{
16
    public $tubes = [];
17
18
    public static function cleanTube($tube)
19
    {
20
        return str_replace('.', '_', $tube);
21
    }
22
23
    public static function createFromGlobals()
24
    {
25
        /** @var \OSInet\Beanstalkd\Munin\QueueAgePlugin $instance */
26
        $instance = parent::createFromGlobals();
27
28
        $tubes = getenv('TUBES') ?: 'default';
29
        $tubesArray = explode(' ', $tubes);
30
31
        foreach ($tubesArray as $tube) {
32
            $instance->tubes[$instance->cleanTube($tube)] = $tube;
33
        }
34
35
        return $instance;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function config()
42
    {
43
        $ret = <<<'EOT'
44
graph_title Job Age
45
graph_vlabel Max Age
46
graph_category Beanstalk
47
graph_args --lower-limit 0
48
graph_scale no
49
50
EOT;
51
52
        foreach (array_keys($this->tubes) as $clean) {
53
            $ret .= sprintf("%s_jobs.label %s\n", $clean, $clean);
54
            $ret .= sprintf("%s_jobs.type GAUGE\n", $clean)
55
              . sprintf("%s_jobs.min 0\n", $clean);
56
        }
57
        return $ret;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function data()
64
    {
65
        $server = $this->server;
66
        $ret = '';
67
68
        foreach ($this->tubes as $clean => $tube) {
69
            $server->useTube($tube);
70
            try {
71
                $job = $server->peekReady();
72
            } catch (ServerException $e) {
0 ignored issues
show
Bug introduced by
The class OSInet\Beanstalkd\Munin\ServerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
73
                $job = null;
74
            }
75
76
            $val = isset($job)
77
              ? $server->statsJob($job)['age']
78
              : 0;
79
80
            $ret .= sprintf("%s_jobs.value %d\n", $clean, $val);
81
        }
82
83
        return $ret;
84
    }
85
}
86