@@ -41,6 +41,10 @@ sub new
4141 $this -> Debug(3," " );
4242 $this -> {' rrd' }=();
4343 $this -> {' daemon' }-> {' confFiles' } = [];
44+ $this -> {' counter' }-> {' static' } = 0;
45+ $this -> {' counter' }-> {' dynamic' } = 0;
46+ $this -> {' counter' }-> {' status' } = 0;
47+ $this -> {' counter' }-> {' statistics' } = 0;
4448 return $this ;
4549}
4650
@@ -83,7 +87,7 @@ sub Load
8387 $this -> {' daemon' }-> {' delay' } ||= 10;
8488 $this -> {' daemon' }-> {' timeout' } ||= 5;
8589 $this -> {' daemon' }-> {' sharedmemkey' }||= 20130906;
86-
90+
8791 # Check user and group
8892 $this -> {' daemon' }-> {' gid' } = getgrnam ($this -> {' daemon' }-> {' user' }) || 1000;
8993 $this -> {' daemon' }-> {' uid' } = getpwnam ($this -> {' daemon' }-> {' group' }) || 1000;
@@ -112,6 +116,11 @@ sub Load
112116
113117 # print Data::Dumper->Dump([$this->{'rrdlist'}]);
114118
119+ # manage page common parameters
120+ $this -> {' web' }-> {' page' }-> {' icon' } ||= ' img/logo.png' ;
121+ $this -> {' web' }-> {' page' }-> {' menutitle' } ||= ' RPi-Monitor' ;
122+ $this -> {' web' }-> {' page' }-> {' pagetitle' } ||= ' RPi-Monitor' ;
123+
115124 # manage menu
116125 foreach (@{$this -> {' web' }-> {' status' }}) {
117126 $_ -> {' name' } and push (@{$this -> {' menu' }-> {' status' }}, $_ -> {' name' });
@@ -127,6 +136,7 @@ sub Load
127136 ) or die $! ;
128137
129138 if ( $this -> {' show' } ) {
139+ $Data::Dumper::Indent = 1;
130140 die Data::Dumper-> Dump([$this ]);
131141 }
132142}
@@ -135,43 +145,69 @@ sub LoadFile
135145{
136146 my $this = shift ;
137147 my $confFile = shift ;
148+ my $dict ;
138149 $this -> Debug(2," Loading file: $confFile " );
139150
140151 open ( FILE, $confFile )
141152 or die " Error while openning configuration file \" $confFile \" because $! \n " ;
142153 while (<FILE>){
143154 chomp ;
144155 / ^\s *#|^$ / and next ; # Ignore comments
156+ / ^include=(.*)/ and -f $1 and push ($this -> {' daemon' }-> {' confFiles' }, $1 ) and next ;
145157 my ($key , $value ) = /^([^=]*)=(.*)/; # Extract key and value
146158 my @leaves =split (' \.' , $key ); # Explode key and construct config hash
147159 my $tree =$this ;
160+ my $previous ;
148161 my $current ;
149162 my $next ;
150- while (scalar (@leaves ) > 0) {
151- $current ||= shift (@leaves );
152- $next = shift (@leaves );
153- if ( $next =~ / ^\d +$ / ) {
154- if ($current =~ / ^\d +$ / ) {
155- @{$tree }[$current -1] ||= [];
156- $tree =@{$tree }[$current -1];
163+ my $root ;
164+ while (scalar (@leaves ) > 0) { # While @leaves contains data
165+ $current ||= shift (@leaves ); # If root is empty, shift the first element of @leaves to $current
166+ $root ||= $current ;
167+ if ( $root eq ' web' ) { $root = $current }
168+ $next = shift (@leaves ); # Shift the first element of @leaves in $next
169+ if ( $next =~ / ^\d +$ / ) { # If $next is an integer -> we are in an array
170+ if ($current =~ / ^\d +$ / ) { # If $current is an integer -> we are in an array
171+ @{$tree }[$current -1] ||= []; # If the branch is not an array, create an empty array
172+ $tree =@{$tree }[$current -1]; # Define this array as $tree root
157173 }
158174 else {
159- $tree -> {$current } ||= [];
160- $tree =$tree -> {$current };
175+ $tree -> {$current } ||= []; # If the branch is not an array, create an empty array
176+ $tree =$tree -> {$current }; # Define this array as $tree root
161177 }
162- } else {
163- if ($current =~ / ^\d +$ / ) {
164- @{$tree }[$current -1] ||= {};
165- $tree =@{$tree }[$current -1];
178+ } else { # If $next is not an integer -> We are in a hash
179+ if ($current =~ / ^\d +$ / ) { # If $current is an integer -> we are in an array
180+ my $index = $current ;
181+
182+ # Manage per file indexes unicity
183+ if ( ($root eq " static" ) ||
184+ ($root eq " dynamic" ) ||
185+ ( ( ($root eq ' status' ) ||
186+ ($root eq ' statistics' ) ) &&
187+ ($previous eq " content" ) ) )
188+ {
189+ # If $index is not in $dict, add it with counter++
190+ if ( ! $dict -> {$root }[$current ] )
191+ {
192+ $dict -> {$root }[$current ] = ++$this -> {' counter' }-> {$root };
193+ }
194+ $index = $dict -> {$root }[$current ];
195+ }
196+
197+ @{$tree }[$index -1] ||= {}; # If the branch is not an array, create an empty hash
198+ $tree =@{$tree }[$index -1]; # Define this hash as $tree root
166199 }
167- else {
168- $tree -> {$current } ||= {};
169- $tree =$tree -> {$current };
200+ else { # We are in a hash
201+ $tree -> {$current } ||= {}; # If the branch is not an array, create an empty hash
202+ $tree =$tree -> {$current }; # Define this hash as $tree root
170203 }
171204 }
172205 if ( ($next eq ' rrd' ) and $value ) { push (@{$this -> {' rrd' }},$tree ) };
206+ $previous = $current ;
173207 $current = $next ;
174208 }
209+
210+ # Add value
175211 if ($current =~ / ^\d +$ / ) {
176212 @{$tree }[$current -1] = $value ;
177213 }
@@ -242,6 +278,7 @@ sub new
242278 " /statistics.json" ,
243279 " /menu.json" ,
244280 " /friends.json" ,
281+ " /page.json" ,
245282 " /version.json" ,
246283 " /favicon.ico" ,
247284
@@ -439,6 +476,7 @@ sub Run
439476 $this -> {' status' } = encode_json(\@{$configuration -> {' web' }-> {' status' }});
440477 $this -> {' statistics' } = encode_json(\@{$configuration -> {' web' }-> {' statistics' }});
441478 $this -> {' friends' } = encode_json(\@{$configuration -> {' web' }-> {' friends' }});
479+ $this -> {' page' } = encode_json(\%{$configuration -> {' web' }-> {' page' }});
442480 $this -> {' static' } = encode_json(\%{$monitor -> {' static' }});
443481 $this -> {' menu' } = encode_json(\%{$configuration -> {' menu' }});
444482 $this -> {' version' } = " {\" version\" :\" $configuration ->{'version'}\" }" ;
@@ -519,6 +557,10 @@ sub Run
519557 or warn $! ;
520558 print FILE encode_json \@{$configuration -> {' web' }-> {' status' }} ;
521559 close (FILE);
560+ open (FILE, " > $configuration ->{'daemon'}->{'webroot'}/page.json" )
561+ or warn $! ;
562+ print FILE encode_json \%{$configuration -> {' web' }-> {' page' }} ;
563+ close (FILE);
522564 open (FILE, " > $configuration ->{'daemon'}->{'webroot'}/statistics.json" )
523565 or warn $! ;
524566 print FILE encode_json \@{$configuration -> {' web' }-> {' statistics' }} ;
@@ -935,6 +977,7 @@ sub Run
935977package main;
936978use strict;
937979use POSIX;
980+ use Sys::Hostname;
938981
939982sub help
940983{
@@ -1077,6 +1120,7 @@ $pidfile and &daemonize;
10771120my $monitor = Monitor-> new();
10781121# Process data that should be extracted once, before the server starts
10791122$monitor -> Process($configuration ,' static' );
1123+ $monitor -> {' static' }-> {' hostname' } = hostname();
10801124
10811125$SIG {INT } = sub { stop(); }; # catch Ctrl+C
10821126
0 commit comments