Projet

Général

Profil

Révision a8819e19

IDa8819e19934cee14ffcb9c488639c17cf740631f
Parent d0255045
Enfant e192d8d9

Ajouté par DisasteR il y a presque 14 ans

Code cleanning/refactoring // Stats for root server and virtual servers

Voir les différences:

plugins/other/murmur_ice_users
1 1
#!/usr/bin/php
2 2
<?php
3
/*
4
: << =cut
5

  
6
=head1 NAME
7

  
8
murmur_ice_users
9

  
10
=head1 DESCRIPTION
11

  
12
This plugin monitors users on a Mumble server (a.k.a. murmur).
13
It uses PHP and ICE to to query murmur. On debian, you can install mumble-server-web
14
to get the dependencies installed and set up.
15

  
16

  
17
=head1 CONFIGURATION
18

  
19
You can specify a different ip:port in the munin-node config file as follow:
20

  
21
    [murmur_ice_users]
22
    env.host 127.0.0.1
23
    env.port 6502
24

  
25

  
26
=head1 AUTHOR
27

  
28
Thomas L?veil
29

  
30
=head1 LICENSE
31

  
32
Permission to use, copy, and modify this software with or without fee
33
is hereby granted, provided that this entire notice is included in
34
all source code copies of any software which is or includes a copy or
35
modification of this software.
36

  
37
THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
38
IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
39
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
40
MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
41
PURPOSE.
42

  
43
=head1 CONTRIBUTE
44

  
45
find this plugin on github at http://github.com/VolatileMesh/munin-plugins
46

  
47
=head1 MAGIC MARKERS
48

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

  
52
=head1 VERSION
53

  
54
    1.1
55

  
56
=head1 CHANGELOG
57

  
58
=head2 1.0 - 2009/05/04
59

  
60
initial release
61

  
62
=head2 1.1 - 2011/01/13
63

  
64
fix to make it work with murmur v1.2.2
65

  
66
    
67
=cut
3
  /**
4
**   =head1 NAME
5
** 
6
**   murmur_ice_users
7
** 
8
**   =head1 DESCRIPTION
9
** 
10
**   This plugin monitors users on a Mumble server (a.k.a. murmur).
11
**   It uses PHP and ICE to to query murmur. On debian, you can install mumble-server-web
12
**   to get the dependencies installed and set up.
13
** 
14
** 
15
**   =head1 CONFIGURATION
16
** 
17
**   You can specify a different ip:port in the munin-node config file as follow:
18
** 
19
**   [murmur_ice_users]
20
**   env.host 127.0.0.1
21
**   env.port 6502
22
**   env.IceProfile xxxxx
23
**   env.IceSecret xxxxx
24
** 
25
**   =head1 AUTHOR
26
** 
27
**   Original Author Thomas L�veil
28
**   Modification by DisasteR - 2011/04/19
29
** 
30
**   =head1 LICENSE
31
** 
32
**   Permission to use, copy, and modify this software with or without fee
33
**   is hereby granted, provided that this entire notice is included in
34
**   all source code copies of any software which is or includes a copy or
35
**   modification of this software.
36
** 
37
**   THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
38
**   IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
39
**   REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
40
**   MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
41
**   PURPOSE.
42
** 
43
**
44
**   =head1 MAGIC MARKERS
45
**
46
**   #%# family=contrib 
47
**   #%# capabilities=autoconf
48
**
49
**
50
**   =head1 VERSION
51
**   
52
**       1.2
53
**   
54
**   =head1 CHANGELOG
55
**   
56
**   =head2 1.0 - 2009/05/04
57
**   
58
**   initial release
59
**   
60
**   =head2 1.1 - 2011/01/13
61
**   
62
**   fix to make it work with murmur v1.2.2
63
**   
64
**   =head3 1.2 - 2011/04/19
65
**
66
**   Code cleaning and refactoring
67
**   Stats for root server and Virtual Servers
68
**       
69
**   =cut
68 70
*/
69

  
70

  
71
    
72
    
71 73
$host = "127.0.0.1";
72 74
$port = "6502";
75
$IceProfile = 'Murmur123';
76
$IceSecret = array( 'secret' => 'mumble42' );
73 77

  
74 78
if (isset($_ENV['host'])) $host = $_ENV['host'];
75 79
if (isset($_ENV['port'])) $port = $_ENV['port'];
80
if (isset($_ENV['IceProfile'])) $IceProfile = $_ENV['IceProfile'];
81
if (isset($_ENV['IceScret'])) $IceSecret = array( 'secret' => $_ENV['IceScret'] );
76 82

  
77 83
if (count($argv)==1) {
78
    do_count();
79
} 
84
  do_count();
85
 } 
80 86

  
81 87
switch ($argv[1]) 
82
{
83
	case 'autoconf':
84
		do_autoconf();
85
	break;
88
  {
89
  case 'autoconf':
90
    do_autoconf();
91
    break;
86 92
	
87
	case 'config':
88
		do_config();
89
	break;
93
  case 'config':
94
    do_config();
95
    break;
90 96
	
91
	default:
92
		do_count();
93
	break;
94
}
97
  default:
98
    do_count();
99
    break;
100
  }
95 101
exit(1);
96 102

  
103
function IceConnect()
104
{
105
  global $ICE, $host, $port, $IceProfile, $IceSecret;
106
  try 
107
    {
108
      Ice_loadProfile($IceProfile);
109
      $iceproxy = $ICE->stringToProxy("Meta:tcp -h $host -p $port");
110
      $metaServer = $iceproxy->ice_checkedCast("::Murmur::Meta")->ice_context($IceSecret);
111
    }
112
  catch (Exception $e)
113
    {
114
      fwrite(STDERR, $e."\n");
115
      exit(1);
116
    }
117
  return $metaServer;
118
}
97 119

  
98 120
function do_autoconf()
99 121
{
100
	global $ICE, $host, $port;
101
	try
102
	{
103
		Ice_loadProfile();
104
		$iceproxy = $ICE->stringToProxy("Meta:tcp -h $host -p $port");
105
		$metaServer = $iceproxy->ice_checkedCast("::Murmur::Meta");
106
		fwrite(STDOUT, "yes\n");
107
		exit(0);
108
	}
109
	catch (Exception $e)
110
	{
111
		fwrite(STDOUT, "no\n");
112
		exit(1);
113
	}
122
  $metaServer = null;
123

  
124
  $metaServer = IceConnect();
125
  if ($metaServer != null)
126
    {
127
      fwrite(STDOUT, "yes\n");
128
      exit(0);
129
    }
130
  fwrite(STDOUT, "no\n");
131
  exit(1);
114 132
}
115 133

  
116
function do_config()
134
function do_config_header()
117 135
{
118
	fwrite(STDOUT, "graph_title Mumble Users\n");
119
	fwrite(STDOUT, "graph_vlabel Connected Users\n");
120
	fwrite(STDOUT, "graph_category VoIP\n");
121
	fwrite(STDOUT, "graph_info This graph shows the number of connected users on a murmur server\n");
122
	fwrite(STDOUT, "maxusers.label Maximum number of users allowed\n");
123
	fwrite(STDOUT, "maxusers.type GAUGE\n");
124
	fwrite(STDOUT, "online.label connected users\n");
125
	fwrite(STDOUT, "online.type GAUGE\n");
126
	exit(0);
136
  fwrite(STDOUT, "graph_title Mumble Users\n");
137
  fwrite(STDOUT, "graph_vlabel Connected Users\n");
138
  fwrite(STDOUT, "graph_category VoIP\n");
139
  fwrite(STDOUT, "graph_info This graph shows the number of connected users on a murmur server\n");
140
  fwrite(STDOUT, "total_maxusers.label Maximum allowed users\n");
141
  fwrite(STDOUT, "total_maxusers.type GAUGE\n");
142
  fwrite(STDOUT, "total_online.label Connected users\n");
143
  fwrite(STDOUT, "total_online.type GAUGE\n");
127 144
}
128 145

  
129
function do_count()
146
function do_config_data()
130 147
{
131
	global $ICE, $host, $port;
132
	
133
	$totalMaxUsers="U";
134
	$totalConnectedUsers="U";
135

  
136
	try 
137
	{
138
		Ice_loadProfile();
139
		$iceproxy = $ICE->stringToProxy("Meta:tcp -h $host -p $port");
140
		$metaServer = $iceproxy->ice_checkedCast("::Murmur::Meta");
141
		$AdefaultConf = $metaServer->getDefaultConf();
142
		
143
		$AvirtualServer = $metaServer->getBootedServers();
144
		
145
		$totalMaxUsers = 0;
146
		foreach ($AvirtualServer as $numserver=>$s) 
147
		{
148
			$maxusers = $s->getConf('users');
149
			if (!$maxusers) $maxusers = $AdefaultConf['users'];
150
			$totalMaxUsers += intval($maxusers);
151
            
152
            $tree = $s->getTree();
153
            //print_r($tree);
154
			$connectedUsers = countConnectedUsers($s->getTree());			
155
			$totalConnectedUsers += $connectedUsers;
156
		}
148
  global $ICE, $IceSecret;
149
  
150
  try 
151
    {
152
      $metaServer = IceConnect();
153
      $AdefaultConf = $metaServer->getDefaultConf();
157 154
		
155
      $AvirtualServer = $metaServer->getAllServers();
158 156

  
159
		fwrite(STDOUT,  "maxusers.value ".$totalMaxUsers."\n");
160
		fwrite(STDOUT,  "online.value ".$totalConnectedUsers."\n");
161
		exit(0);
162
	}
163
	catch (Exception $e)
157
      foreach ($AvirtualServer as $numserver=>$s) 
164 158
	{
165
		fwrite(STDERR, $e."\n");
166
		exit(1);
159
	  $serverid = $s->ice_context($IceSecret)->id();
160
	  $servename = $s->ice_context($IceSecret)->getConf( 'registername');
161
	  $servename = preg_replace('/[^a-zA-Z0-9-#\$%&@\.+=§\/\\\]/', ' ', $servename);
162
	  $servename = trim(preg_replace('/\s+/', ' ', $servename));
163

  
164
	  fwrite(STDOUT,  "vserver_".$serverid."_maxusers.label ".$servename." Max users\n");
165
	  fwrite(STDOUT,  "vserver_".$serverid."_maxusers.type GAUGE\n");
166
	  fwrite(STDOUT,  "vserver_".$serverid."_online.label ".$servename." Connected users\n");
167
	  fwrite(STDOUT,  "vserver_".$serverid."_online.type GAUGE\n");
167 168
	}
169
      exit(0);
170
    }
171
  catch (Exception $e)
172
    {
173
      fwrite(STDERR, $e."\n");
174
      exit(1);
175
    }
168 176
}
169 177

  
170
function countConnectedUsers($channelTree) 
178
function do_config()
171 179
{
172
	$count = intval(count($channelTree->users));
173
	
174
	if (isset($channelTree->children))
180
  do_config_header();
181
  do_config_data();
182
  exit(0);
183
}
184

  
185
function do_count()
186
{
187
  global $ICE, $IceSecret;
188

  
189
  $totalMaxUsers="0";
190
  $totalConnectedUsers="0";
191

  
192
  try 
193
    {
194
      $metaServer = IceConnect();
195
      $AdefaultConf = $metaServer->getDefaultConf();
196
		
197
      $AvirtualServer = $metaServer->getAllServers();
198

  
199
      foreach ($AvirtualServer as $numserver=>$s) 
175 200
	{
176
		foreach ($channelTree->children as $c)
177
		{
178
			$count += countConnectedUsers($c);
179
		}
201
	  $maxusers = $s->ice_context($IceSecret)->getConf( 'users' );
202
	  if (!$maxusers) $maxusers = $AdefaultConf['users'];
203
	  $totalMaxUsers += intval($maxusers);
204

  
205
	  if ($s->ice_context( $IceSecret )->isRunning())
206
	    {
207
	      $connectedUsers = count($s->ice_context($IceSecret)->getUsers());
208
	      $totalConnectedUsers += intval($connectedUsers);
209
	    }
210
	  else
211
	      $connectedUsers = 0;
212

  
213
	  $serverid = $s->ice_context($IceSecret)->id();
214

  
215
	  fwrite(STDOUT,  "vserver_".$serverid."_maxusers.value ".$maxusers."\n");
216
	  fwrite(STDOUT,  "vserver_".$serverid."_online.value ".$connectedUsers."\n");
180 217
	}
181
	
182
	return $count;
218
		
219
      fwrite(STDOUT,  "total_maxusers.value ".$totalMaxUsers."\n");
220
      fwrite(STDOUT,  "total_online.value ".$totalConnectedUsers."\n");
221
      exit(0);
222
    }
223
  catch (Exception $e)
224
    {
225
      fwrite(STDERR, $e."\n");
226
      exit(1);
227
    }
183 228
}
184 229

  
185 230
?>

Formats disponibles : Unified diff