aboutsummaryrefslogtreecommitdiffstats
path: root/ubnt_unifi.php
blob: 648a4c05ebd0ea31705c22f64292111b3b6b8547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/php
<?php

// Munin monitoring plugin for Ubiquiti Unifi AP system.

//$controller = "unifi.company.com";
//$hosts = "ap01.wireless.company.lan ap02.wireless.company.lan ap03.wireless.company.lan ap04.wireless.company.lan";


$controller = getenv('controller');
$hosts = getenv('devices');
$timeout = getenv('timeout');
$retry = getenv('retry');
$maxproc = getenv('maxproc');
$devnetw = getenv('devnet');

$replace_chars = array("\"","\$","@","^","`",",","|","%",";",".","~","(",")","/","\\","{","}",":","?","[","]","=","+","#","!","-",);	// Special chars replace

function print_header($inp){ // prints Munin-config data from processed data array

	$cf  = "multigraph unifi_".$inp['g_multi']."\n";
        $cf .= "host_name ".$inp['g_controller']."\n";
        $cf .= "graph_title ".$inp['g_title']."\n";
        $cf .= "graph_args --base 1000 \n";
        $cf .= "graph_vlabel ".$inp['g_vlabel']."\n";
        $cf .= "graph_category ".$inp['g_category']."\n";
        $cf .= "graph_info ".$inp['g_info']."\n";
	if(isset($inp['g_order'])){ $cf .= "graph_order ".$inp['g_order']."\n"; }

	foreach($inp['head'] as $key => $val){
		$cf .= "unifi_".$val['name'].".label ".$val['label']."\n";
		$cf .= "unifi_".$val['name'].".draw  ".$val['draw']."\n";
		$cf .= "unifi_".$val['name'].".info  ".$val['info']."\n";
		if(isset($val['type'])){ $cf .= "unifi_".$val['name'].".type  ".$val['type']."\n"; }
		if(isset($val['min'])) { $cf .= "unifi_".$val['name'].".min  ".$val['min']."\n";  }
		if(isset($val['cdef'])){ $cf .= "unifi_".$val['name'].".cdef  ".$val['cdef']."\n"; }
		if(isset($val['graph'])){ $cf .= "unifi_".$val['name'].".graph  ".$val['graph']."\n"; }
		if(isset($val['max'])){ $cf .= "unifi_".$val['name'].".max  ".$val['max']."\n"; }
		if(isset($val['negative'])){ $cf .= "unifi_".$val['name'].".negative  ".$val['negative']."\n"; }
	}
	$cf .= "\n";
	echo iconv("UTF-8", "ISO-8859-2", $cf), PHP_EOL;
}


function print_data($inp) {

        if(!array_key_exists('data', $inp) or !array_key_exists('g_multi', $inp)){
                return;
        }
	
	$pf  = "multigraph unifi_".$inp['g_multi']."\n";
	foreach($inp['data'] as $key => $val){
		$pf .= "unifi_".$val['name'].".value ".$val['value']."\n" ;
	}
	$pf .= "\n";
	
	echo iconv("UTF-8", "ISO-8859-2", $pf), PHP_EOL;

}

function count_wl_networks($inp){ //Count wireless networks from interfade data
	$num = -10;
        foreach($inp as $key => $val){          // jump to the end of wl interface list
		if(strpos($key, "iso.3.6.1.4.1.41112.1.6.1.2.1.1.") !== false){
                	if(is_numeric(explode(": ", $val)[1]) and $num < explode(": ", $val)[1]){
                        	$num = explode(": ", $val)[1];
                        }
                }
	}
        $num = $num+1;	//Because the snmp counts from 0
	
	if($num > 0){
		return $num;
	} else {
		return -1;
	}
}


function collect_radio_summary($inp,$host){
	global $controller, $replace_chars;
	$ret = array();
        if(!array_key_exists($host, $inp)){
                return $ret;
        }

	if(isset($host) and $host !== null and $host != "" ){
                $ret['g_multi'] = "radio_".str_replace( array(".", ":"), "_" ,$controller).".".str_replace( array(".", ":"), "_" ,$host);
		$ret['g_controller'] = $controller;
		$location = str_replace("\"", "", explode(": ", $inp[$host]["iso.3.6.1.2.1.1.6.0"])[1]);
        if( $location != "Unknown" and $location != "" ){ $ret['g_title'] = "Unifi Clients on: ".$location ; } // if the Location is not filled in Controller settings, use the hostname or ip address
		else { $ret['g_title'] = "Unifi Clients on: ".$host; };
                $ret['g_vlabel'] = "Users";
                $ret['g_category'] = "wl_clients_ap";
                $ret['g_info'] = "ubnt_wireless";
	} else {
		$ret['g_multi'] = "radio_".str_replace( array(".", ":"), "_" ,$controller);
                $ret['g_controller'] = $controller;
		$ret['g_title'] = "Unifi Clients on: $controller (total)";
		$ret['g_vlabel'] = "Users";
		$ret['g_category'] = "Wl_clients_all";
		$ret['g_info'] = "ubnt_wireless";
	}
				
        $ret['head'][0]['name'] = "sum_clients";
        $ret['head'][0]['label'] = "Total clients";
        $ret['head'][0]['draw'] = "LINE1.2";
        $ret['head'][0]['info'] = "Total Clients";
        $ret['head'][0]['type'] = "GAUGE";
        $ret['head'][0]['min']	= "0";

        $ret['head'][1]['name'] = "2g_clients";
        $ret['head'][1]['label'] = "2.4Ghz";
        $ret['head'][1]['draw'] = "LINE1.2";
        $ret['head'][1]['info'] = "2.4Ghz Clients";
        $ret['head'][1]['type'] = "GAUGE";
        $ret['head'][1]['min']  = "0";

        $ret['head'][2]['name'] = "5g_clients";
        $ret['head'][2]['label'] = "5Ghz";
        $ret['head'][2]['draw'] = "LINE1.2";
        $ret['head'][2]['info'] = "2.4Ghz Clients";
        $ret['head'][2]['type'] = "GAUGE";
        $ret['head'][2]['min']  = "0";


	if(isset($host) and $host !== null and $host != "" ){	// trim raw data array to current device (in $host) or use the whole array when calculating controller's data 
		$temp = $inp;
		unset($inp);
		$inp = array($host => $temp[$host]);
		unset($temp);
	}

	foreach($inp as $key => $val){

		for($i=1; $i<=count_wl_networks($inp[$key]); $i++){	//Collect clients by band.
			if( explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.4.".$i])[1] < 15 ){					//2.4Ghz client
				$ret['data'][1]['name'] = "2g_clients";
				@$ret['data'][1]['value'] += explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.8.".$i])[1];
			} else {													// 5Ghz clients
                $ret['data'][2]['name'] = "5g_clients";
                @$ret['data'][2]['value'] += explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.8.".$i])[1];
			}
		}
                $ret['data'][0]['name'] = "sum_clients";
                $ret['data'][0]['value'] = $ret['data'][1]['value'] + $ret['data'][2]['value'];



                for($i=1; $i<=count_wl_networks($inp[$key]); $i++){     //Collect clients by SSID.

                        foreach($ret['data'] as $key2 => $val2){	//find if ssid is already used
                                if($ret['data'][$key2]['name'] == str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]) ){
                                        break;
                                }
                        }
					//found, update record
                        if($ret['data'][$key2]['name'] == str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]) ){
                                $ret['data'][$key2]['value'] += explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.8.".$i])[1];

                        } else {	//not found, new record
                                $ret['data'][] = array( "name" => str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                                                        "value" => explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.8.".$i])[1] 
                                                );
                                $ret['head'][] = array( "name" => str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                                                        "label" => str_replace("\"", "",explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1] ),
                                                        "draw"  => "LINE1.2",
                                                        "info"  => explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1],
                                                        "type"	=> "GAUGE",
                                                        "min"	=> "0",
                                                );

                        }
                }
        }


return $ret;
}




function collect_netw_summary($inp,$host){	//network information
        global $controller, $replace_chars;
        $ret = array();
        if(!array_key_exists($host, $inp)){
                return $ret;
        }
	
	if(isset($host) and $host !== null and $host != "" ){ // When showing the Ap's summary.
              $temp = $inp;
              unset($inp);
              $inp = array($host => $temp[$host]);
              unset($temp);
              $multiplier = 8;
              $divider = 1;
      	} else {			//because the normal INTEGER(32) would be overflowed
              $multiplier = 8*1024*4;	// When showing the controller's summary
              $divider = 1024*4;
      	}


        if(isset($host) and $host !== null and $host != "" ){
                $ret['g_multi'] = "netw_".str_replace( array(".", ":"), "_" ,$controller).".".str_replace( array(".", ":"), "_" ,$host);
                $ret['g_controller'] = $controller;
                $location = str_replace("\"", "", explode(": ", $inp[$host]["iso.3.6.1.2.1.1.6.0"])[1]);
		if($location != "Unknown" and $location != ""){ $ret['g_title'] = "Network Usage on: ".$location; }
                else{$ret['g_title'] = "Network Usage on: ".$host;}
                $ret['g_vlabel'] = "bits in(-) / out(+) per second";
                $ret['g_category'] = "Wl_netw_ap";
                $ret['g_info'] = "ubnt_network";
                $ret['g_order'] = "rx_all tx_all rx_2g tx_2g rx_5g tx_5g";

        } else {
                $ret['g_multi'] = "netw_".str_replace( array(".", ":"), "_" ,$controller);
                $ret['g_controller'] = $controller;
                $ret['g_title'] = "Netwok Usage on: $controller (total)";
                $ret['g_vlabel'] = "bits in(-) / out(+) per second";
                $ret['g_category'] = "Wl_netw_all";
                $ret['g_info'] = "ubnt_network";
                $ret['g_order'] = "rx_all tx_all rx_2g tx_2g rx_5g tx_5g";
        }
         
                $ret['head'][0]['name'] = "rx_all";
                $ret['head'][0]['label'] = "RxTotal (bps)";
                $ret['head'][0]['draw'] = "LINE1.2";
                $ret['head'][0]['info'] = "Total Received";
                $ret['head'][0]['type'] = "DERIVE";
                $ret['head'][0]['min']  = "0";
                $ret['head'][0]['graph']  = "no";
                $ret['head'][0]['cdef']  = "unifi_rx_all,$multiplier,*";
                $ret['head'][0]['max']  = "1000000000";

                $ret['head'][1]['name'] = "tx_all";
                $ret['head'][1]['label'] = "Total (bps)";
                $ret['head'][1]['draw'] = "LINE1.2";
                $ret['head'][1]['info'] = "Total Sent";
                $ret['head'][1]['type'] = "DERIVE";
                $ret['head'][1]['min']  = "0";
                $ret['head'][1]['cdef']  = "unifi_tx_all,$multiplier,*";
                $ret['head'][1]['max']  = "1000000000";
                $ret['head'][1]['negative']  = "unifi_rx_all";

                $ret['head'][2]['name'] = "rx_2g";
                $ret['head'][2]['label'] = "2G (bps)";
                $ret['head'][2]['draw'] = "LINE1.2";
                $ret['head'][2]['info'] = "Total Received";
                $ret['head'][2]['type'] = "DERIVE";
                $ret['head'][2]['min']  = "0";
                $ret['head'][2]['graph']  = "no";
                $ret['head'][2]['cdef']  = "unifi_rx_2g,$multiplier,*";
                $ret['head'][2]['max']  = "1000000000";

                $ret['head'][3]['name'] = "tx_2g";
                $ret['head'][3]['label'] = "2G (bps)";
                $ret['head'][3]['draw'] = "LINE1.2";
                $ret['head'][3]['info'] = "Total Sent";
                $ret['head'][3]['type'] = "DERIVE";
                $ret['head'][3]['min']  = "0";
                $ret['head'][3]['cdef']  = "unifi_tx_2g,$multiplier,*";
                $ret['head'][3]['max']  = "1000000000";
                $ret['head'][3]['negative']  = "unifi_rx_2g";

                $ret['head'][4]['name'] = "rx_5g";
                $ret['head'][4]['label'] = "5G (bps)";
                $ret['head'][4]['draw'] = "LINE1.2";
                $ret['head'][4]['info'] = "Total Received";
                $ret['head'][4]['type'] = "DERIVE";
                $ret['head'][4]['min']  = "0";
                $ret['head'][4]['graph']  = "no";
                $ret['head'][4]['cdef']  = "unifi_rx_5g,$multiplier,*";
                $ret['head'][4]['max']  = "1000000000";

                $ret['head'][5]['name'] = "tx_5g";
                $ret['head'][5]['label'] = "5G (bps)";
                $ret['head'][5]['draw'] = "LINE1.2";
                $ret['head'][5]['info'] = "Total Sent";
                $ret['head'][5]['type'] = "DERIVE";
                $ret['head'][5]['min']  = "0";
                $ret['head'][5]['cdef']  = "unifi_tx_5g,$multiplier,*";
                $ret['head'][5]['max']  = "1000000000";
                $ret['head'][5]['negative']  = "unifi_rx_5g";


        foreach($inp as $key => $val){

                for($i=1; $i<=count_wl_networks($inp[$key]); $i++){     //Collect netw_bytes by band and direction.
                        if( explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.4.".$i])[1] < 15 ){                                 //2.4Ghz client
                                $ret['data'][2]['name'] = "rx_2g";		       
                                @$ret['data'][2]['value'] += round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.10.".$i])[1]) / $divider);
                                $ret['data'][3]['name'] = "tx_2g";
                                @$ret['data'][3]['value'] += round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.16.".$i])[1]) / $divider);

                        } else {                                                                                                        // 5Ghz clients
                                $ret['data'][4]['name'] = "rx_5g";                     
                                @$ret['data'][4]['value'] += round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.10.".$i])[1]) / $divider);
                                $ret['data'][5]['name'] = "tx_5g";
                                @$ret['data'][5]['value'] += round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.16.".$i])[1]) / $divider);
                        }
                }
                $ret['data'][0]['name'] = "rx_all";
                $ret['data'][0]['value'] = $ret['data'][2]['value'] + $ret['data'][4]['value'];
                $ret['data'][1]['name'] = "tx_all";
                $ret['data'][1]['value'] = $ret['data'][3]['value'] + $ret['data'][5]['value'];



                for($i=1; $i<=count_wl_networks($inp[$key]); $i++){     //Collect netw_bytes by SSID.

                        foreach($ret['data'] as $key2 => $val2){        //find if ssid is already used
                                if($ret['data'][$key2]['name'] == "rx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]) ){
                                        break;
                                }
                        }
                                        //ssid found, update record
                        if($ret['data'][$key2]['name'] == "rx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]) ){
                                $ret['data'][$key2]['value'] += round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.10.".$i])[1]) / $divider);

                        } else {       //ssid not found, new record
                                $ret['data'][] = array( "name" => "rx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                                                        "value" => round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.10.".$i])[1]) / $divider) ,
                                                );
                                $ret['head'][] = array( "name"  => "rx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                                                        "label" => "RX_".str_replace("\"", "",explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1] )." (bps)",
                                                        "draw"  => "LINE1.2",
                                                        "info"  => "Rx_".explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1],
                                                        "type"  => "DERIVE",
                                                        "min"   => "0",
                                                        "graph"	=> "no",
                                                        "cdef"	=> "unifi_rx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]).",$multiplier,*",
                                                        "max"	=> "1000000000",
                                                );

                        }

			foreach($ret['data'] as $key2 => $val2){        //find if ssid is already used
                    		if($ret['data'][$key2]['name'] == "tx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]) ){
                            		break;
                    		}
            		}
                     		       //ssid found, update record
            		if($ret['data'][$key2]['name'] == "tx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]) ){
                    		$ret['data'][$key2]['value'] += round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.16.".$i])[1]) / $divider);

			} else {       //ssid not found, new record
                    		$ret['data'][] = array( "name" => "tx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                                			"value" => round((explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.16.".$i])[1]) / $divider) ,
                                            );
                    		$ret['head'][] = array( "name"  => "tx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                        				"label" => "".str_replace("\"", "",explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1] )." (bps)",
                                        	    	"draw"  => "LINE1.2",
                                            		"info"  => "Tx_".explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1],
                                	            	"type"  => "DERIVE",
                                        	    	"min"   => "0",
                                            		"cdef"  => "unifi_tx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]).",$multiplier,*",
                                            		"max"   => "1000000000",
                                                    "negative" => "unifi_rx_".str_replace($replace_chars, "_", explode(": ", $inp[$key]["iso.3.6.1.4.1.41112.1.6.1.2.1.6.".$i])[1]),
                                            );
            		}


                }
        }


return $ret;
}


$hosts = explode(" ", $hosts);
$netw = @explode('/', $devnetw)[0];
$mask = @explode('/', $devnetw)[1];
$hosts2 = array();

if($mask != 0 ){
	for($i=1; $i<(1 << (32 - $mask)); $i++ ){		// fetch ip addresses from given network and mask
		$hosts2[] = long2ip((ip2long($netw) & ~((1 << (32 - $mask)) -1))  +$i  ) ;
	}
}

foreach($hosts as $key => $val){			// delete addresses which are given by hostname
	if(in_array(gethostbyname($val), $hosts2)){
		unset($hosts2[ array_keys($hosts2, gethostbyname($val))[0] ]);
	}

}
$hosts = array_merge($hosts, $hosts2);
unset($hosts2);

$numhost = count($hosts);
$shm_key = ftok($argv[0], 'c');
$shm = shmop_open($shm_key, "c", 0640, ceil($numhost/$maxproc)*32768);
$sf = sem_get($shm_key,1,0640,1);
$child = array();
$raw = array();
$hostsr = $hosts;
unset($hosts);
$hostst = array();


for($i=0,$j=0; $i<$numhost; $i++){	//Sorting addresses for child-processes
	
	if($j >= $maxproc){		//With permutation
	    $j = 0;
	}
	if(array_key_exists($j, $hostst) === FALSE){
		$hostst[$j] = array();
	}

	$hostst[$j] = array_merge($hostst[$j], array($hostsr[$i]));    //With permutation
	$j++;
}




for ($p=0; $p<$maxproc; $p++){		//Starts child processes to retrieve SNMP data.
	
	unset($hosts);
	$hosts = array();
	if(array_key_exists($p,$hostst)){
		$hosts = $hostst[$p];
	}


        $pid = pcntl_fork();

        if($pid == -1){
             die('could not fork');
        }

        else if($pid){			// we are the parent process
                $child[$p] = $pid;
        } 

	else {			// child
		$raw=array();
		foreach($hosts as $key => $val){			// get raw snmp data from unifi devices
			if($hosts[$key] == ""){
				unset($hosts[$key]);
			}
			if($val != "") {
				$raw[$val] = @snmp2_real_walk($val, "public", ".1.3.6.1.4.1.41112.1.6.1.2.1", $timeout*1000, $retry ); 		// wl network info
				$raw[$val]["iso.3.6.1.2.1.1.6.0"] = @snmp2_get($val, "public", ".1.3.6.1.2.1.1.6.0", $timeout*1000, $retry ) ;	// location info
				$raw[$val]["iso.3.6.1.2.1.1.1.0"] = @snmp2_get($val, "public", ".1.3.6.1.2.1.1.1.0", $timeout*1000, $retry ) ;	// descr. info
			}
		        if( !isset($raw[$val]["iso.3.6.1.4.1.41112.1.6.1.2.1.1.1"]) ){
                        unset($raw[$val]);
                        unset($hosts[$key]);
		        }
			
			$null="";
			for($f=0; $f<(32768 - strlen(@json_encode($raw))); $f++)
			{
				$null .= "\0";   	//Because the json_decode() error.
			}

			sem_acquire($sf);
			while(ord(shmop_read($shm, 0, 0)) ) {continue;}  //waiting for master to pull the data
			shmop_write($shm, @json_encode($raw).$null, 0);
			sem_release($sf);
		}
	exit;
	}

}
unset($hostst);
unset($hosts);

function numchild($child, $n){	//How many child process is alive
        $l=0;
        for($i=0; $i<$n; $i++){
            if($child[$i] > 0){
                $l++;
            }
        }
        return $l;
}


while(numchild($child, $maxproc)){	//Receive the raw data segments and wait for child processes

	for($p=0; $p<$maxproc; $p++){
		if( abs($pid = pcntl_waitpid($child[$p], $status, WNOHANG)) > 0) {//Protect against Zombie children
			$child[$p] = 0;
		}
    }

	$ret = shmop_read($shm, 0, 0);  //Read from shared memory
	if(ord($ret)){
        $ret = preg_replace('/[[:cntrl:]]/', '', $ret);         // for json_decode
        $raw = @array_merge($raw, @json_decode($ret, true));      
        shmop_write($shm,"\0\0\0\0\0", 0);
	}
	usleep(100);	//
}

sem_remove($sf);
shmop_delete($shm);
shmop_close($shm);
$hosts = $hostsr;
//print_r($raw);
//print_r($hosts);
//$test = collect_netw_summary($raw,"ap12.wireless.lan");
//print_r($test);

if(!is_array($raw)){
    die();
}


if (isset($argv[1]) and $argv[1] == "config"){			// munin config
	print_header(collect_radio_summary($raw,null));
	foreach($hosts as $key => $val){
		print_header(collect_radio_summary($raw,$val));
	}
        print_header(collect_netw_summary($raw,null));
        foreach($hosts as $key => $val){
                print_header(collect_netw_summary($raw,$val));
        }


} else {							// munin data
	print_data(collect_radio_summary($raw,null));
        foreach($hosts as $key => $val){
                print_data(collect_radio_summary($raw,$val));
        }
        print_data(collect_netw_summary($raw,null));
        foreach($hosts as $key => $val){
                print_data(collect_netw_summary($raw,$val));
        }

}


echo "\n";

?>