Projet

Général

Profil

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

root / plugins / icecast / icecast2_simple @ 17f78427

Historique | Voir | Annoter | Télécharger (1,84 ko)

1
#!/usr/bin/env ruby
2
#
3
# Plugin author: Gunnar Wolf <gwolf@gwolf.org>
4
#
5
# You are hereby granted authorization to copy, use, modify, distribute,
6
# and in general do anything you please with this plugin. It is too simple
7
# even to GPL-protect it.
8
#
9
# This plugin expects to receive via environment variables:
10
#
11
# icecast_host - Which host to monitor (default: 127.0.0.1)
12
# icecast_username - Username to connect with (default: admin)
13
# icecast_password - Password to connect with (default: hackme)
14
# icecast_realm - Realm to connect with (default: 'Icecast2 server')
15
#
16
require 'hpricot'
17
require 'open-uri'
18

    
19
def get_conf
20
  # Default values
21
  conf = {:host => '127.0.0.1', :port => 8000,
22
    :username => 'admin', :password => 'hackme' }
23
  conf.keys.each do |key|
24
    env_key = sprintf('icecast_%s', key)
25
    conf[key] = ENV[env_key] if ENV.has_key?(env_key)
26
  end
27
  conf
28
end
29

    
30
def get_data(conf)
31
  begin
32
    data = Hpricot(open(sprintf('http://%s:%s/admin/stats',
33
                                conf[:host], conf[:port]),
34
                        :http_basic_authentication=>[conf[:username],
35
                                                     conf[:password]]))
36
  rescue OpenURI::HTTPError
37
    puts "Cannot connect: HTTP connection error"
38
    exit 1
39
  end
40
  data
41
end
42

    
43
def get_values(data)
44
  vals = {}
45
  [:sources, :clients].each do |key|
46
    elem = data/key
47
    if elem.nil?
48
      vals[key] = 0
49
    else
50
      vals[key] = elem.innerHTML
51
    end
52
  end
53
  vals
54
end
55

    
56
data = get_data(get_conf)
57
vals = get_values(data)
58

    
59
if ARGV[0] == 'autoconf'
60
  puts 'yes'
61
elsif ARGV[0] == 'config'
62
  puts "graph_title Total sources and clients for Icecast"
63
  puts "graph_vlabel listeners"
64
  puts "graph_category streaming"
65
  puts "sources.label Total number of sources"
66
  puts "clients.label Total number of clients"
67
else
68
  puts "sources.value " + vals[:sources]
69
  puts "clients.value " + vals[:clients]
70
end