Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / lighttpd / lighttpd_ @ c6f88968

Historique | Voir | Annoter | Télécharger (3,92 ko)

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# vim: set fileencoding=utf-8
4
"""
5

    
6
=head1 NAME
7

    
8
Munin plugin to monitor lighttpd web-server.
9

    
10

    
11
=head1 CONFIGURATION
12

    
13
Configuration parameters:
14

    
15
    [lighttpd_]
16
    env.status_url - url of lighty's server-status
17
                     (optional, default is http://127.0.0.1/server-status)
18
    env.username - username to provide if status_url requires authentication
19
                   (optional, default - no authentication)
20
    env.password - password to provide if status_url requires authentication
21
                   (optional, default - no authentication)
22
    env.auth_type - the authentication mechanism to use -- either 'basic' (default) or 'digest'.
23

    
24
Note: If HTTP authentication is required you should specify both username and password.
25

    
26

    
27
=head1 INSTALLATION
28

    
29
Copy file to directory /usr/share/munin/plugins/
30
Because this plugin has "suggest" capability the last step is to run
31

    
32
    munin-node-configure --suggest --shell | sh -x
33

    
34

    
35
=head1 AUTHOR
36

    
37
Copyright Igor Borodikhin
38

    
39

    
40
=head1 LICENSE
41

    
42
GNU General Public License v3.0 only
43

    
44
SPDX-License-Identifier: GPL-3.0-only
45

    
46

    
47
=head1 MAGIC MARKERS
48

    
49
  #%# family=contrib
50
  #%# capabilities=autoconf suggest
51

    
52
=cut
53
"""
54

    
55
import os
56
import sys
57
import urllib2
58

    
59

    
60
program = sys.argv[0]
61
graph_type = program[program.rfind("_") + 1:]
62
graph_types = {
63
    "accesses": [
64
        {
65
            "title": "Total accesses",
66
            "type": "COUNTER",
67
            "args": "--base 1000 -l 0",
68
            "fields": ["accesses"]
69
        }
70
    ],
71
    "kbytes": [
72
        {
73
            "title": "Total kBytes",
74
            "type": "COUNTER",
75
            "args": "--base 1024 -l 0",
76
            "fields": ["kbytes"]
77
        }
78
    ],
79
    "uptime": [
80
        {
81
            "title": "Uptime",
82
            "type": "GAUGE",
83
            "args": "--base 1000 -l 0",
84
            "fields": ["uptime"]
85
        }
86
    ],
87
    "status": [
88
        {
89
            "title": "Status",
90
            "type": "GAUGE",
91
            "args": "--base 1000 -l 0",
92
            "fields": ["busy", "idle"]
93
        }
94
    ]
95
}
96

    
97

    
98
if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
99
    print("yes")
100
elif len(sys.argv) == 2 and sys.argv[1] == "config":
101
    if graph_type not in graph_types.keys():
102
        raise Exception("Unknown graph type '%s'" % graph_type)
103
    params = graph_types[graph_type]
104
    for item in params:
105
        print("graph_title %s" % item["title"])
106
        print("graph_category webserver")
107
        for field in item["fields"]:
108
            print("%s.label %s" % (field, field))
109
            print("%s.type %s" % (field, item["type"]))
110
        print("graph_args %s" % item["args"])
111
elif len(sys.argv) == 2 and sys.argv[1] == "suggest":
112
    for item in graph_types.keys():
113
        print(item)
114
else:
115
    status_url = os.environ.get('status_url', 'http://127.0.0.1/server-status')
116

    
117
    request = urllib2.Request("%s?auto" % status_url)
118
    if "username" in os.environ and "password" in os.environ:
119
        mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
120
        mgr.add_password(None, status_url, os.environ["username"], os.environ["password"])
121
        if os.environ.get("auth_type", "basic") == "digest":
122
            auth = urllib2.HTTPDigestAuthHandler(mgr)
123
        else:
124
            auth = urllib2.HTTPBasicAuthHandler(mgr)
125
        opener = urllib2.build_opener(auth)
126
        urllib2.install_opener(opener)
127
    info = urllib2.urlopen(request).read()
128
    data = {}
129
    for line in info.split("\n"):
130
        try:
131
            (title, value) = line.split(": ")
132
            data[title] = value
133
        except ValueError:
134
            pass
135

    
136
    if graph_type == "accesses":
137
        print("accesses.value %s" % data["Total Accesses"])
138
    elif graph_type == "kbytes":
139
        print("kbytes.value %s" % data["Total kBytes"])
140
    elif graph_type == "uptime":
141
        print("uptime.value %s" % str(float(data["Uptime"]) / 86400))
142
    elif graph_type == "status":
143
        print("busy.value %s" % data["BusyServers"])
144
        print("idle.value %s" % data["IdleServers"])