OSDN Git Service

Generated Makefile.am.
[ultramonkey-l7/sslproxy.git] / src / sslproxyadm
1 #!/usr/bin/perl  --
2 ######################################################################
3 # sslproxyadm
4 # SSLproxy process administration command.
5 #
6 # Copyright (C) 2008  NTT COMWARE Corporation.
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; either version 2 of the
11 # License, or (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301 USA
22 ######################################################################
23 use strict;
24 use warnings;
25
26 # logger test flag.
27 our $LOGGER_DEBUG = 0;
28
29 # sslproxyadm define value.
30 our $OK = 0;
31 our $NG = 1;
32 our $ON = 1;
33 our $OFF = 0;
34 our $SSLPROXY_PATH = "/usr/sbin/";
35 our $SSLPROXY = "sslproxy";
36 our $LOGGER_SECTION_KEY = "logger";
37 our $TARGET_SECTION_KEY = "target_.*";
38 our $LOGROTATE_CMD = "/usr/sbin/logrotate";
39 our $LOGROTATE_CONF_FILE = "/etc/logrotate.d/sslproxy";
40 our $LOGROTATE_STAT_FILE = "/var/lib/logrotate.status";
41
42 # sslproxyadm default parameter value.
43 our $CMD_OPT = "unknown";
44 our $CONFIG_FILE = "/etc/l7vs/sslproxy/sslproxyadm.cf";
45 our $LOG_FILE = "/var/log/l7vs/sslproxy/sslproxyadm.log";
46 our $LOG_LEVEL = "warn";
47
48 # Set library path.
49 $ENV{LD_LIBRARY_PATH} = "/usr/local/lib/";
50
51 # sslproxyadm global data table
52 our %sslproxyadm_data = ();
53 our %target_data = ();
54 our %process_data = ();
55
56 # main code.
57         # Start main routine.
58         &do_main;
59
60         # Here is not executed. However, it exists for safety. 
61         &do_exit($NG);
62 # main code end.
63
64 #!
65 # sslproxyadm command main routine.
66 #
67 # command argment check and command execution.
68 #
69 sub do_main
70 {
71         #--------- DEBUG LOG ---------#
72         my $argc = @ARGV;
73         &print_log("file", "DEBUG", "common", "10050001", 
74                    "in_function : do_main : ".
75                    "argc = $argc, ".
76                    "argv = @ARGV");
77         #------- DEBUG LOG END -------#
78
79         my $result = $NG;
80
81         # Check command argment count.
82         if (@ARGV == 2) {
83                 $CONFIG_FILE = $ARGV[1];
84                 #--------- DEBUG LOG ---------#
85                 &print_log("file", "DEBUG", "common", "10050002", 
86                            "Command arg count is normal.");
87                 #------- DEBUG LOG END -------#
88         } elsif (@ARGV == 1) {
89                 #--------- DEBUG LOG ---------#
90                 &print_log("file", "DEBUG", "common", "10050003", 
91                            "Command arg count is normal. Use default config file.");
92                 #------- DEBUG LOG END -------#
93         } else {
94                 &print_log("fcerr", "ERROR", "common", "40050001", 
95                            "Command arg count is abnormal.");
96                 &usage;
97                 #--------- DEBUG LOG ---------#
98                 &print_log("file", "DEBUG", "common", "10050004", 
99                            "out_function : do_main : Check argment NG");
100                 #------- DEBUG LOG END -------#
101                 &do_exit($NG);
102         }
103         $CMD_OPT = $ARGV[0];
104         #--------- DEBUG LOG ---------#
105         &print_log("file", "DEBUG", "common", "10050005", 
106                    "function : do_main : Check argment OK");
107         #------- DEBUG LOG END -------#
108
109         # Read sslproxyadm configration file.
110         ($result) = &read_adm_config;
111         if ($result == $NG) {
112                 &print_log("fcerr", "ERROR", "common", "40050002", 
113                            "Config file read error. file = $CONFIG_FILE");
114                 #--------- DEBUG LOG ---------#
115                 &print_log("file", "DEBUG", "common", "10050006", 
116                            "out_function : do_main : read_adm_config NG");
117                 #------- DEBUG LOG END -------#
118                 &do_exit($NG);
119         }
120         #--------- DEBUG LOG ---------#
121         &print_log("file", "DEBUG", "common", "10050007", 
122                    "function : do_main : read_adm_config OK");
123         #------- DEBUG LOG END -------#
124
125         # Set logger configuration.
126         ($result) = &set_log;
127         if ($result == $NG) {
128                 &print_log("fcerr", "ERROR", "common", "40050003", 
129                            "Logger setting error.");
130                 #--------- DEBUG LOG ---------#
131                 &print_log("file", "DEBUG", "common", "10050008", 
132                            "out_function : do_main : set_log NG");
133                 #------- DEBUG LOG END -------#
134                 &do_exit($NG);
135         }
136         #--------- DEBUG LOG ---------#
137         &print_log("file", "DEBUG", "common", "10050009", 
138                    "function : do_main : set_log OK");
139         #------- DEBUG LOG END -------#
140
141         # Check command option.
142         if ($CMD_OPT ne "start" &&
143             $CMD_OPT ne "stop" &&
144             $CMD_OPT ne "restart" &&
145             $CMD_OPT ne "reload" &&
146             $CMD_OPT ne "check" &&
147             $CMD_OPT ne "status" &&
148             $CMD_OPT ne "config") { 
149                 &print_log("fcerr", "ERROR", "common", "40050004", 
150                            "Invalid command. command = $CMD_OPT");
151                 &usage;
152                 #--------- DEBUG LOG ---------#
153                 &print_log("file", "DEBUG", "common", "10050010", 
154                            "out_function : do_main : Check command NG");
155                 #------- DEBUG LOG END -------#
156                 &do_exit($NG);
157         }
158         #--------- DEBUG LOG ---------#
159         &print_log("file", "DEBUG", "common", "10050011", 
160                    "function : do_main : Check command OK");
161         #------- DEBUG LOG END -------#
162
163         # Check target configration file.
164         ($result) = &check_target_config;
165         if ($result == $NG) {
166                 &print_log("fcerr", "ERROR", "common", "40050005", 
167                            "Check target config file error.");
168                 #--------- DEBUG LOG ---------#
169                 &print_log("file", "DEBUG", "common", "10050012", 
170                            "out_function : do_main : check_target_config NG");
171                 #------- DEBUG LOG END -------#
172                 &do_exit($NG);
173         }
174         #--------- DEBUG LOG ---------#
175         &print_log("file", "DEBUG", "common", "10050013", 
176                    "function : do_main : check_target_config OK");
177         #------- DEBUG LOG END -------#
178
179         # Check target configration file format command execution.
180         if ($CMD_OPT eq "config") {
181                 ($result) = &do_config;
182                 #--------- DEBUG LOG ---------#
183                 &print_log("file", "DEBUG", "common", "10050014", 
184                            "function : do_main : Command execute END. command = $CMD_OPT");
185                 #------- DEBUG LOG END -------#
186                 if ($result == $NG) {
187                         &print_log("fcerr", "ERROR", "common", "40050006", 
188                                    "Command execute error. command = $CMD_OPT");
189                         #--------- DEBUG LOG ---------#
190                         &print_log("file", "DEBUG", "common", "10050015", 
191                                    "out_function : do_main : Command execute NG");
192                         #------- DEBUG LOG END -------#
193                         &do_exit($NG);
194                 }
195                 #--------- DEBUG LOG ---------#
196                 &print_log("file", "DEBUG", "common", "10050016", 
197                            "out_function : do_main : Command execute OK. command = $CMD_OPT");
198                 #------- DEBUG LOG END -------#
199                 &do_exit($OK);
200         }
201
202         # Get sslproxy process status.
203         ($result) = &get_process_status;
204         if ($result == $NG) {
205                 &print_log("fcerr", "ERROR", "common", "40050007", 
206                            "Get process status error.");
207                 #--------- DEBUG LOG ---------#
208                 &print_log("file", "DEBUG", "common", "10050017", 
209                            "out_function : do_main : get_process_status NG");
210                 #------- DEBUG LOG END -------#
211                 &do_exit($NG);
212         }
213         #--------- DEBUG LOG ---------#
214         &print_log("file", "DEBUG", "common", "10050018", 
215                    "function : do_main : get_process_status OK");
216         #------- DEBUG LOG END -------#
217
218         # Command execution.
219         if ($CMD_OPT eq "start") {
220                 ($result) = &do_start;
221                 #--------- DEBUG LOG ---------#
222                 &print_log("file", "DEBUG", "common", "10050019", 
223                            "function : do_main : Command execute END. command = $CMD_OPT");
224                 #------- DEBUG LOG END -------#
225         } elsif ($CMD_OPT eq "stop") {
226                 ($result) = &do_stop;
227                 #--------- DEBUG LOG ---------#
228                 &print_log("file", "DEBUG", "common", "10050020", 
229                            "function : do_main : Command execute END. command = $CMD_OPT");
230                 #------- DEBUG LOG END -------#
231         } elsif ($CMD_OPT eq "restart") {
232                 ($result) = &do_restart;
233                 #--------- DEBUG LOG ---------#
234                 &print_log("file", "DEBUG", "common", "10050021", 
235                            "function : do_main : Command execute END. command = $CMD_OPT");
236                 #------- DEBUG LOG END -------#
237         } elsif ($CMD_OPT eq "reload") {
238                 ($result) = &do_reload;
239                 #--------- DEBUG LOG ---------#
240                 &print_log("file", "DEBUG", "common", "10050022", 
241                            "function : do_main : Command execute END. command = $CMD_OPT");
242                 #------- DEBUG LOG END -------#
243         } elsif ($CMD_OPT eq "check") {
244                 ($result) = &do_check;
245                 #--------- DEBUG LOG ---------#
246                 &print_log("file", "DEBUG", "common", "10050023", 
247                            "function : do_main : Command execute END. command = $CMD_OPT");
248                 #------- DEBUG LOG END -------#
249         } elsif ($CMD_OPT eq "status") {
250                 ($result) = &do_status;
251                 #--------- DEBUG LOG ---------#
252                 &print_log("file", "DEBUG", "common", "10050024", 
253                            "function : do_main : Command execute END. command = $CMD_OPT");
254                 #------- DEBUG LOG END -------#
255         } else {
256                 # Here is not executed. However, it exists for safety. 
257                 &print_log("fcerr", "ERROR", "common", "40050008", 
258                            "Invalid command. command = $CMD_OPT");
259                 &usage;
260                 #--------- DEBUG LOG ---------#
261                 &print_log("file", "DEBUG", "common", "10050025", 
262                            "out_function : do_main : Check command NG");
263                 #------- DEBUG LOG END -------#
264                 &do_exit($NG);
265         }
266         if ($result == $NG) {
267                 &print_log("fcerr", "ERROR", "common", "40050009", 
268                            "Command execute error. command = $CMD_OPT");
269                 #--------- DEBUG LOG ---------#
270                 &print_log("file", "DEBUG", "common", "10050026", 
271                            "out_function : do_main : Command execute NG");
272                 #------- DEBUG LOG END -------#
273                 &do_exit($NG);
274         }
275         #--------- DEBUG LOG ---------#
276         &print_log("file", "DEBUG", "common", "10050027", 
277                    "out_function : do_main : Command execute OK. command = $CMD_OPT");
278         #------- DEBUG LOG END -------#
279         &do_exit($OK)
280 }
281
282 #!
283 # Read sslproxyadm configration file.
284 #
285 # @retval       OK/NG   read result
286 #
287 sub read_adm_config
288 {
289         #--------- DEBUG LOG ---------#
290         &print_log("file", "DEBUG", "config", "10030001", 
291                    "in_function : read_adm_config");
292         #------- DEBUG LOG END -------#
293         my $result = $NG;
294
295         %sslproxyadm_data = ();
296         ($result) = &read_config($CONFIG_FILE, "adm");
297         if ($result == $NG) {
298                 &print_log("file", "ERROR", "config", "40030001", 
299                            "Read sslproxyadm configration file error. file = $CONFIG_FILE");
300         }
301
302         # for debug.
303         #&print_table_data(%sslproxyadm_data);
304
305         #--------- DEBUG LOG ---------#
306         &print_log("file", "DEBUG", "config", "10030002", 
307                    "out_function : read_adm_config : result = $result");
308         #------- DEBUG LOG END -------#
309         return ($result);
310 }
311
312 #!
313 # Check target configration files. exist or not.
314 #
315 # @retval       OK/NG   check result
316 #
317 sub check_target_config
318 {
319         #--------- DEBUG LOG ---------#
320         &print_log("file", "DEBUG", "config", "10030003", 
321                    "in_function : check_target_config");
322         #------- DEBUG LOG END -------#
323         my $result = $NG;
324
325         # Search target configration file.
326         foreach my $section (sort keys(%sslproxyadm_data)) {
327                 #--------- DEBUG LOG ---------#
328                 &print_log("file", "DEBUG", "config", "10030004", 
329                            "Search section. ".
330                            "section = $section");
331                 #------- DEBUG LOG END -------#
332                 my $target_conf_file = "";
333                 # Check target section.
334                 next if ($section !~ /^$TARGET_SECTION_KEY$/);
335                 # Get target configration file name.
336                 $target_conf_file = $sslproxyadm_data{$section}{"conf_file"};
337                 # Open target configration file for exist check.
338                 if(!open(CONFIG, "<$target_conf_file")) {
339                         &print_log("file", "ERROR", "config", "40030002", 
340                                    "Cannot open target config file. file = $target_conf_file");
341                         $result = $NG;
342                         last;
343                 }
344                 close(CONFIG);
345                 $result = $OK;
346                 #--------- DEBUG LOG ---------#
347                 &print_log("file", "DEBUG", "config", "10030005", 
348                            "Target config file open OK. ".
349                            "target_conf_file = $target_conf_file");
350                 #------- DEBUG LOG END -------#
351         }
352
353         #--------- DEBUG LOG ---------#
354         &print_log("file", "DEBUG", "config", "10030006", 
355                    "out_function : check_target_config : ".
356                    "result = $result");
357         #------- DEBUG LOG END -------#
358         return ($result);
359 }
360
361 #!
362 # Read specified configration file.
363 #
364 # @param[in]    conf_file       configration file
365 # @param[in]    conf_kind       configration file kind(adm or target)
366 # @retval       OK/NG           read result
367 #
368 sub read_config
369 {
370         # Argment.
371         my ($conf_file, $conf_kind) = (@_);
372         my $result = $OK;
373         #--------- DEBUG LOG ---------#
374         &print_log("file", "DEBUG", "config", "10030007", 
375                    "in_function : read_config : ".
376                    "conf_file = $conf_file ".
377                    "conf_kind = $conf_kind");
378         #------- DEBUG LOG END -------#
379
380         # Check config file kind. "adm" or "target"
381         if ($conf_kind ne "adm" && $conf_kind ne "target") {
382                 &print_log("file", "ERROR", "config", "40030003", 
383                            "Invalid config file kind. kind = $conf_kind");
384                 $result = $NG;
385         # Open config file.
386         } elsif(!open(CONFIG, "<$conf_file")) {
387                 &print_log("file", "ERROR", "config", "40030004", 
388                            "Cannot open config file. file = $conf_file");
389                 $result = $NG;
390         } else {
391                 # Read data and Set to data table.
392                 my $line_data = "";
393                 my $section_flag = $OFF;
394                 my $now_section = "";
395                 my $now_line = 0;
396                 my $sec_key;
397                 # Set section keyword.
398                 if ($conf_kind eq "adm") {
399                         $sec_key = $TARGET_SECTION_KEY;
400                 } elsif ($conf_kind eq "target") {
401                         $sec_key = $SSLPROXY;
402                 }
403                 while ($line_data = <CONFIG>) {
404                         # Read one line data.
405                         $now_line++;
406                         # Skip Null line and comment line.
407                         next if ($line_data =~ /^\s*$|^\s*#/);
408                         # Cut space, tab, \n.
409                         $line_data =~ s/\s|\t|\n//g;
410                         #--------- DEBUG LOG ---------#
411                         &print_log("file", "DEBUG", "config", "10030008", 
412                                    "Read line data [$now_line] = $line_data");
413                         #------- DEBUG LOG END -------#
414
415                         # Check section line. (include [logger] [$sec_key])
416                         if ($line_data =~ 
417                             /^\[$LOGGER_SECTION_KEY\]$|^\[$sec_key\]$/) {
418                                 # Check start of first section.
419                                 $section_flag = $ON;
420                                 # Cut [ and ].
421                                 $line_data =~ s/\[|\]//g;
422                                 # Check duplication of section name. "adm" or "target"
423                                 if ($conf_kind eq "adm") {
424                                         while (my ($section_name, $section_data) = each (%sslproxyadm_data)) {
425                                                 #--------- DEBUG LOG ---------#
426                                                 &print_log("file", "DEBUG", "config", "10030009", 
427                                                            "Check section name. ".
428                                                            "read line = $line_data, ".
429                                                            "saved data = $section_name");
430                                                 #------- DEBUG LOG END -------#
431                                                 if ($line_data eq $section_name) {
432                                                         &print_log("file", "ERROR", "config", "40030005", 
433                                                                    "Section name is duplicated. ".
434                                                                    "section = $section_name Line:$now_line");
435                                                         $result = $NG;
436                                                         last;
437                                                 }
438                                         }
439                                 } elsif ($conf_kind eq "target") {
440                                         while (my ($section_name, $section_data) = each (%target_data)) {
441                                                 #--------- DEBUG LOG ---------#
442                                                 &print_log("file", "DEBUG", "config", "10030010", 
443                                                            "Check section name. ".
444                                                            "read line = $line_data, ".
445                                                            "saved data = $section_name");
446                                                 #------- DEBUG LOG END -------#
447                                                 if ($line_data eq $section_name) {
448                                                         &print_log("file", "ERROR", "config", "40030006", 
449                                                                    "Section name is duplicated. ".
450                                                                    "section = $section_name Line:$now_line");
451                                                         $result = $NG;
452                                                         last;
453                                                 }
454                                         }
455                                 }
456                                 if ($result == $NG) {
457                                         #--------- DEBUG LOG ---------#
458                                         &print_log("file", "DEBUG", "config", "10030011", 
459                                                    "Check section name NG.");
460                                         #------- DEBUG LOG END -------#
461                                         last;
462                                 }
463                                 $now_section = $line_data;
464                                 #--------- DEBUG LOG ---------#
465                                 &print_log("file", "DEBUG", "config", "10030012", 
466                                            "New section found. section = $now_section");
467                                 #------- DEBUG LOG END -------#
468                                 next;
469                         }
470
471                         # Check parameter line. (include "=")
472                         if ($line_data =~ /=/) {
473                                 # Out of section.
474                                 if ($section_flag == $OFF) {
475                                         &print_log("file", "ERROR", "config", "40030007", 
476                                                    "Ivalid parameter line.  ".
477                                                    "Out of section. Line:$now_line");
478                                         $result = $NG;
479                                         last;
480                                 }
481                                 # Split by "=" delimiter to key and value.
482                                 my @param = split /=/, $line_data, 2;
483                                 # Check key is "".
484                                 if ($param[0] eq "") {
485                                         &print_log("file", "ERROR", "config", "40030008", 
486                                                    "Ivalid parameter line.  ".
487                                                    "Parameter key is NULL. Line:$now_line");
488                                         $result = $NG;
489                                         last;
490                                 }
491                                 # Cut char value's ".
492                                 $param[1] =~ s/"//g;
493                                 # Parameter data set to data table. "adm" or "target"
494                                 if ($conf_kind eq "adm") {
495                                         $sslproxyadm_data{$now_section}{$param[0]} = $param[1];
496                                 } elsif ($conf_kind eq "target") {
497                                         $target_data{$now_section}{$param[0]} = $param[1];
498                                 }
499                                 #--------- DEBUG LOG ---------#
500                                 &print_log("file", "DEBUG", "config", "10030013", 
501                                            "Parameter set. ".
502                                            "section = $now_section, ".
503                                            "key = $param[0], ".
504                                            "value = $param[1]");
505                                 #------- DEBUG LOG END -------#
506                                 next;
507                         }
508
509                         # Ivalid line. 
510                         &print_log("file", "ERROR", "config", "40030009", 
511                                    "Ivalid parameter line.  ".
512                                    "Unknown line. Line:$now_line");
513                         $result = $NG;
514                         last;
515                 }
516                 close(CONFIG);
517                 #--------- DEBUG LOG ---------#
518                 &print_log("file", "DEBUG", "config", "10030014", 
519                            "Config file read and Set data END.");
520                 #------- DEBUG LOG END -------#
521
522                 # Check read and data set result.
523                 # $result is OK? section is exist?
524                 if ($result == $OK) {
525                         # Check section count. "adm" or "target"
526                         my $logger_section_count = 0;
527                         my $section_count = 0;
528                         if ($conf_kind eq "adm") {
529                                 foreach my $adm_section (sort keys(%sslproxyadm_data)) {
530                                         if ($adm_section =~ /^$LOGGER_SECTION_KEY$/) {
531                                                 $logger_section_count++;
532                                         }
533                                         if ($adm_section =~ /^$sec_key$/) {
534                                                 $section_count++;
535                                         }
536                                 }
537                         } elsif ($conf_kind eq "target") {
538                                 foreach my $target_section (sort keys(%target_data)) {
539                                         if ($target_section =~ /^$LOGGER_SECTION_KEY$/) {
540                                                 $logger_section_count++;
541                                         }
542                                         if ($target_section =~ /^$sec_key$/) {
543                                                 $section_count++;
544                                         }
545                                 }
546                         }
547                         if ($logger_section_count == 0) {
548                                 &print_log("file", "ERROR", "config", "40030010", 
549                                            "[logger] section nothing in config file. ".
550                                            "file = $conf_file");
551                                 $result = $NG;
552                         } elsif ($section_count == 0) {
553                                 &print_log("file", "ERROR", "config", "40030011", 
554                                            "[$sec_key] section nothing in config file. ".
555                                            "file = $conf_file");
556                                 $result = $NG;
557                         } else {
558                                 #--------- DEBUG LOG ---------#
559                                 &print_log("file", "DEBUG", "config", "10030015", 
560                                            "Read and data set result OK.");
561                                 #------- DEBUG LOG END -------#
562                         }
563                 }
564         }
565
566         # for debug.
567         #&print_table_data(%sslproxyadm_data);
568         #&print_table_data(%target_data);
569
570         #--------- DEBUG LOG ---------#
571         &print_log("file", "DEBUG", "config", "10030016", 
572                    "out_function : read_config : result = $result");
573         #------- DEBUG LOG END -------#
574         return ($result);
575 }
576
577 #!
578 # Get sslproxy process status.
579 #
580 # Collect sslproxy process info and Check status.
581 # and Judge sslproxy process status.
582 #
583 # @retval       OK/NG   get result
584 #
585 sub get_process_status
586 {
587         #--------- DEBUG LOG ---------#
588         &print_log("file", "DEBUG", "status", "10020001", 
589                    "in_function : get_process_status");
590         #------- DEBUG LOG END -------#
591
592         my $result = $OK;
593
594         # Collect SSLproxy process information.
595         ($result) = &collect_process_info;
596         if ($result == $NG) {
597                 &print_log("file", "ERROR", "status", "40020001", 
598                            "Collect sslproxy process info error.");
599         } else {
600                 #--------- DEBUG LOG ---------#
601                 &print_log("file", "DEBUG", "status", "10020002", 
602                            "Check sslproxy process start.");
603                 #------- DEBUG LOG END -------#
604                 foreach my $conf_target (sort keys(%sslproxyadm_data)) {
605                         # Get target id from sslproxyadm_data
606                         # Check target section. (other is skip)
607                         next if ($conf_target !~ /^$TARGET_SECTION_KEY$/);
608
609                         # Check list for sslproxy process status.
610                         # is_start              Starting or Stopped
611                         # cmp_config            match or not
612                         # start_info            file exist or not
613                         # cmp_start_info        match or not
614                         my $is_start = $NG;
615                         my $cmp_config = $NG;
616                         my $start_info = $NG;
617                         my $cmp_start_info = $NG;
618                         # proc_status           judge result
619                         my $proc_status = $NG;
620
621                         # Check is_start. Exist target id in process_data or not.
622                         #--------- DEBUG LOG ---------#
623                         &print_log("file", "DEBUG", "status", "10020003", 
624                                    "Check is_start. target = $conf_target");
625                         #------- DEBUG LOG END -------#
626                         foreach my $proc_target (sort keys(%process_data)) {
627                                 #--------- DEBUG LOG ---------#
628                                 &print_log("file", "DEBUG", "status", "10020004", 
629                                            "Check process = $proc_target");
630                                 #------- DEBUG LOG END -------#
631                                 if ($proc_target eq $conf_target) {
632                                         #--------- DEBUG LOG ---------#
633                                         &print_log("file", "DEBUG", "status", "10020005", 
634                                                    "$proc_target is Starting.");
635                                         #------- DEBUG LOG END -------#
636                                         $is_start = $OK;
637                                         # save pid.
638                                         $sslproxyadm_data{$proc_target}{"pid"} = 
639                                                 $process_data{$proc_target}{"pid"};
640                                         # Set known sslproxy process flag.
641                                         # (Not set process is unknown sslproxy process)
642                                         $process_data{$proc_target}{"known"} = $ON;
643                                         last;
644                                 }
645                         }
646                         $sslproxyadm_data{$conf_target}{"is_start"} = $is_start;
647
648                         # Check cmp_config. Compare target conf file name.
649                         # (Starting process only)
650                         if ($is_start == $OK) {
651                                 #--------- DEBUG LOG ---------#
652                                 &print_log("file", "DEBUG", "status", "10020006", 
653                                            "Check cmp_config. target = $conf_target");
654                                 #------- DEBUG LOG END -------#
655                                 if ($sslproxyadm_data{$conf_target}{"conf_file"} eq 
656                                     $process_data{$conf_target}{"conf_file"}) {
657                                         #--------- DEBUG LOG ---------#
658                                         &print_log("file", "DEBUG", "status", "10020007", 
659                                                    "Config file matched.");
660                                         #------- DEBUG LOG END -------#
661                                         $cmp_config = $OK;
662                                 }
663                                 $sslproxyadm_data{$conf_target}{"cmp_config"} = $cmp_config;
664                         }
665
666                         # Check start_info. Try open start_info file for exist check.
667                         # start_info file name = target_conf_file + "." + target_id
668                         my $start_info_file = $sslproxyadm_data{$conf_target}{"conf_file"}.
669                                               ".".
670                                               $conf_target;
671                         #--------- DEBUG LOG ---------#
672                         &print_log("file", "DEBUG", "status", "10020008", 
673                                    "Check start_info. file = $start_info_file");
674                         #------- DEBUG LOG END -------#
675                         if (open(STARTFILE, "<$start_info_file")) {
676                                 #--------- DEBUG LOG ---------#
677                                 &print_log("file", "DEBUG", "status", "10020009", 
678                                            "Start info file existed.");
679                                 #------- DEBUG LOG END -------#
680                                 $start_info = $OK;
681                                 close(STARTFILE);
682                         }
683                         $sslproxyadm_data{$conf_target}{"start_info"} = $start_info;
684
685                         # Check cmp_start_info. Compare start_info file and conf file.
686                         # (start_info OK only)
687                         if ($start_info == $OK) {
688                                 #--------- DEBUG LOG ---------#
689                                 &print_log("file", "DEBUG", "status", "10020010", 
690                                            "Check cmp_start_info. target = $conf_target");
691                                 #------- DEBUG LOG END -------#
692                                 # Open diff command output.
693                                 my $diff_cmd = "/usr/bin/diff $start_info_file ".
694                                                $sslproxyadm_data{$conf_target}{"conf_file"}.
695                                                " |";
696                                 if (open(DIFFDATA, $diff_cmd)) {
697                                         #--------- DEBUG LOG ---------#
698                                         &print_log("file", "DEBUG", "status", "10020011", 
699                                                    "Get diff command output.");
700                                         #------- DEBUG LOG END -------#
701                                         my @diff_data = <DIFFDATA>;
702                                         my $diff_cnt = @diff_data;
703                                         # Check diff output size. 0 is matched.
704                                         if ($diff_cnt == 0) {
705                                                 #--------- DEBUG LOG ---------#
706                                                 &print_log("file", "DEBUG", "status", "10020012", 
707                                                            "Start info file matched.");
708                                                 #------- DEBUG LOG END -------#
709                                                 $cmp_start_info = $OK;
710                                         }
711                                         close(DIFFDATA);
712                                 }
713                                 $sslproxyadm_data{$conf_target}{"cmp_start_info"} = $cmp_start_info;
714                         }
715
716                         # Judge sslproxy process status. Check normal start/stop state.
717                         #--------- DEBUG LOG ---------#
718                         &print_log("file", "DEBUG", "status", "10020013", 
719                                    "Judge sslproxy process status by check result. ".
720                                    "target = $conf_target");
721                         #------- DEBUG LOG END -------#
722                         if (($is_start == $OK && 
723                             $cmp_config == $OK && 
724                             $start_info == $OK && 
725                             $cmp_start_info == $OK) ||
726                             ($is_start == $NG && 
727                             $start_info == $NG)) {
728                                 #--------- DEBUG LOG ---------#
729                                 &print_log("file", "DEBUG", "status", "10020014", 
730                                            "Result sslproxy process status is OK. ".
731                                            "Normal Starting or Stopped.");
732                                 #------- DEBUG LOG END -------#
733                                 $proc_status = $OK;
734                         }
735                         $sslproxyadm_data{$conf_target}{"proc_status"} = $proc_status;
736
737                         # now_start flag initialize.
738                         $sslproxyadm_data{$conf_target}{"now_start"} = $OFF;
739                 }
740                 #--------- DEBUG LOG ---------#
741                 &print_log("file", "DEBUG", "status", "10020015", 
742                            "Check and Judge sslproxy process status END.");
743                 #------- DEBUG LOG END -------#
744
745                 # for debug.
746                 #&print_table_data(%process_data);
747                 #&print_table_data(%sslproxyadm_data);
748         }
749
750         #--------- DEBUG LOG ---------#
751         &print_log("file", "DEBUG", "status", "10020016", 
752                    "out_function : get_process_status : result = $result");
753         #------- DEBUG LOG END -------#
754         return ($result);
755 }
756
757 #!
758 # Collect sslproxy process information.
759 #
760 # @retval       OK/NG   collect result
761 #
762 sub collect_process_info
763 {
764         #--------- DEBUG LOG ---------#
765         &print_log("file", "DEBUG", "status", "10020017", 
766                    "in_function : collect_process_info");
767         #------- DEBUG LOG END -------#
768
769         my $result = $OK;
770
771         # Collect sslproxy process information from ps command output.
772         # Set to process_data.
773         %process_data = ();
774         my $ps_cmd = "/bin/ps -C $SSLPROXY -o pid=,cmd= |";
775         if(!open(PROCESS, $ps_cmd)) {
776                 &print_log("file", "ERROR", "status", "40020002", 
777                            "Cannot open ps command output.");
778                 $result = $NG;
779         } else {
780                 # Read ps command data and Set to data table.
781                 my $line_data = "";
782                 while ($line_data = <PROCESS>) {
783                         # Read one line data. (one process)
784                         # Cut \n.
785                         $line_data =~ s/^\s*|\n$//g;
786                         #--------- DEBUG LOG ---------#
787                         &print_log("file", "DEBUG", "status", "10020018", 
788                                    "Read one process line data = $line_data");
789                         #------- DEBUG LOG END -------#
790
791                         # Split @param by " " delimiter.
792                         # $param[0] -> pid
793                         # $param[1] -> cmd (ex. "./sslproxy")
794                         # $param[2] -> target id
795                         # $param[3] -> target config file
796                         my @param = split /\s/, $line_data, 4;
797                         # Parameter data set to data table.
798                         $process_data{$param[2]}{"pid"} = $param[0];
799                         $process_data{$param[2]}{"conf_file"} = $param[3];
800                         $process_data{$param[2]}{"known"} = $OFF;
801                         #--------- DEBUG LOG ---------#
802                         &print_log("file", "DEBUG", "status", "10020019", 
803                                    "Parameter set. ".
804                                    "target_id = $param[2], ".
805                                    "pid = $param[0], ".
806                                    "conf_file = $param[3]");
807                         #------- DEBUG LOG END -------#
808                 }
809                 close(PROCESS);
810                 #--------- DEBUG LOG ---------#
811                 &print_log("file", "DEBUG", "status", "10020020", 
812                            "Process info read and Set data END.");
813                 #------- DEBUG LOG END -------#
814
815                 # Check process count.
816                 my $proc_count = keys(%process_data);
817                 if ($proc_count == 0) {
818                         &print_log("file", "INFO", "status", "20020001", 
819                                    "Executing SSLproxy process is nothing.");
820                 }
821
822                 # for debug.
823                 #&print_table_data(%process_data);
824         }
825
826         #--------- DEBUG LOG ---------#
827         &print_log("file", "DEBUG", "status", "10020021", 
828                    "out_function : collect_process_info : result = $result");
829         #------- DEBUG LOG END -------#
830         return ($result);
831 }
832
833 #!
834 # Update sslproxy process status.
835 # Set Starting <-> Stopped status.
836 #
837 # @param[in]    target_id       target id
838 # @param[in]    flag            update kind flag
839 # @retval       OK/NG   update result
840 #
841 sub update_process_status
842 {
843         # Argment.
844         # flag ig ON(Starting) or OFF(Stopped).
845         my ($target_id, $flag) = (@_);
846         my $result = $NG;
847         #--------- DEBUG LOG ---------#
848         &print_log("file", "DEBUG", "status", "10020022", 
849                    "in_function : update_process_status : ".
850                    "target_id = $target_id, ".
851                    "flag = $flag");
852         #------- DEBUG LOG END -------#
853
854         # Create or Delete start information file.
855         ($result) = &manage_start_info($target_id, $flag);
856         if ($result == $OK) {
857                 # Set start status. (is_start)
858                 if ($flag == $ON) {
859                         $sslproxyadm_data{$target_id}{"is_start"} = $OK;
860                         #--------- DEBUG LOG ---------#
861                         &print_log("file", "DEBUG", "status", "10020023", 
862                                    "Set is_start \"OK\". ".
863                                    "target_id = $target_id");
864                         #------- DEBUG LOG END -------#
865                 } else {
866                         $sslproxyadm_data{$target_id}{"is_start"} = $NG;
867                         #--------- DEBUG LOG ---------#
868                         &print_log("file", "DEBUG", "status", "10020024", 
869                                    "Set is_start \"NG\". ".
870                                    "target_id = $target_id");
871                         #------- DEBUG LOG END -------#
872                 }
873                 # Set started flag. (now_start)
874                 $sslproxyadm_data{$target_id}{"now_start"} = $flag;
875         }
876
877         #--------- DEBUG LOG ---------#
878         &print_log("file", "DEBUG", "status", "10020025", 
879                    "out_function : update_process_status : result = $result");
880         #------- DEBUG LOG END -------#
881         return ($result);
882 }
883
884 #!
885 # Manage start information.
886 # Create and Delete start information file.
887 #
888 # @param[in]    target_id       target id
889 # @param[in]    flag            manage kind flag
890 # @retval       OK/NG   manage result
891 #
892 sub manage_start_info
893 {
894         # Argment.
895         # flag ig ON(Starting) or OFF(Stopped).
896         my ($target_id, $flag) = (@_);
897         my $result = $NG;
898         #--------- DEBUG LOG ---------#
899         &print_log("file", "DEBUG", "status", "10020026", 
900                    "in_function : manage_start_info : ".
901                    "target_id = $target_id, ".
902                    "flag = $flag");
903         #------- DEBUG LOG END -------#
904
905         # start_info file name = target_conf_file + "." + target_id
906         my $config_file = $sslproxyadm_data{$target_id}{"conf_file"};
907         my $start_info_file = $config_file.".".$target_id;
908
909         if ($flag == $ON) {
910                 # Copy conf file to start information file.
911                 my $cp_cmd = "/bin/cp -a $config_file $start_info_file";
912                 ($result) = system($cp_cmd);
913                 if ($result != $OK) {
914                         &print_log("file", "ERROR", "status", "40020003", 
915                                    "Copy command execute error. : $!");
916                         $result = $NG;
917                 }
918                 #--------- DEBUG LOG ---------#
919                 &print_log("file", "DEBUG", "status", "10020027", 
920                            "Copy start information file END. ".
921                            "config_file = $config_file, ".
922                            "start_info_file = $start_info_file");
923                 #------- DEBUG LOG END -------#
924         } elsif ($flag == $OFF) {
925                 # Delete start information file.
926                 my $rm_cmd = "/bin/rm -f $start_info_file";
927                 ($result) = system($rm_cmd);
928                 if ($result != $OK) {
929                         &print_log("file", "ERROR", "status", "40020004", 
930                                    "Remove command execute error. : $!");
931                         $result = $NG;
932                 }
933                 #--------- DEBUG LOG ---------#
934                 &print_log("file", "DEBUG", "status", "10020028", 
935                            "Delete start information file END. ".
936                            "config_file = $config_file, ".
937                            "start_info_file = $start_info_file");
938                 #------- DEBUG LOG END -------#
939         }
940
941         #--------- DEBUG LOG ---------#
942         &print_log("file", "DEBUG", "status", "10020029", 
943                    "out_function : manage_start_info : result = $result");
944         #------- DEBUG LOG END -------#
945         return ($result);
946 }
947
948 #!
949 # Start sslproxy process.
950 #
951 # @retval       OK/NG   start result
952 #
953 sub do_start
954 {
955         #--------- DEBUG LOG ---------#
956         &print_log("file", "DEBUG", "control", "10010001", 
957                    "in_function : do_start");
958         #------- DEBUG LOG END -------#
959
960         my $result = $NG;
961         my $start_cnt = 0;
962
963         # Search start target in sslproxyadm_data.
964         foreach my $target (sort keys(%sslproxyadm_data)) {
965                 #--------- DEBUG LOG ---------#
966                 &print_log("file", "DEBUG", "control", "10010002", 
967                            "Search start target. ".
968                            "target = $target");
969                 #------- DEBUG LOG END -------#
970                 # Check target section. (other is skip)
971                 next if ($target !~ /^$TARGET_SECTION_KEY$/);
972
973                 # Start executing, when target status Stopped.
974                 if ($sslproxyadm_data{$target}{"is_start"} == $NG) {
975                         $start_cnt++;
976                         # Start one target process.
977                         ($result) = &start_process($target);
978                         if ($result == $NG) {
979                                 &print_log("file", "ERROR", "control", "40010001", 
980                                            "Start process error. ".
981                                            "target = $target");
982                                 last;
983                         }
984                         #--------- DEBUG LOG ---------#
985                         &print_log("file", "DEBUG", "control", "10010003", 
986                                    "Start target END. ".
987                                    "target = $target");
988                         #------- DEBUG LOG END -------#
989                 } else {
990                         &print_log("file", "WARN", "control", "30010001", 
991                                    "Process already starting. ".
992                                    "target = $target");
993                 }
994         }
995
996         # Check start target count.
997         if ($start_cnt == 0) {
998                 &print_log("file", "WARN", "control", "30010002", 
999                            "Target process not found for start.");
1000         # When process start failed, already Starting process will be Stopped.
1001         } elsif ($result == $NG) {
1002                 #--------- DEBUG LOG ---------#
1003                 &print_log("file", "DEBUG", "control", "10010004", 
1004                            "Start recover target.");
1005                 #------- DEBUG LOG END -------#
1006
1007                 # Search recover target in sslproxyadm_data.
1008                 foreach my $recover_target (sort keys(%sslproxyadm_data)) {
1009                         #--------- DEBUG LOG ---------#
1010                         &print_log("file", "DEBUG", "control", "10010005", 
1011                                    "Search recover target. ".
1012                                    "recover_target = $recover_target");
1013                         #------- DEBUG LOG END -------#
1014                         # Check target section. (other is skip)
1015                         next if ($recover_target !~ /^$TARGET_SECTION_KEY$/);
1016
1017                         # "now_start" flag is "ON" is Stopped. 
1018                         if ($sslproxyadm_data{$recover_target}{"now_start"} == $ON) {
1019                                 my $recover_result = $NG;
1020                                 # Recover(stop) one target process.
1021                                 ($recover_result) = &stop_process($recover_target);
1022                                 if ($recover_result == $NG) {
1023                                         &print_log("file", "ERROR", "control", "40010002", 
1024                                                    "Recover process error. ".
1025                                                    "recover_target = $recover_target");
1026                                 }
1027                                 #--------- DEBUG LOG ---------#
1028                                 &print_log("file", "DEBUG", "control", "10010006", 
1029                                            "Recover target END. ".
1030                                            "recover_target = $recover_target");
1031                                 #------- DEBUG LOG END -------#
1032                         }
1033                 }
1034         }
1035
1036         #--------- DEBUG LOG ---------#
1037         &print_log("file", "DEBUG", "control", "10010007", 
1038                    "out_function : do_start : result = $result");
1039         #------- DEBUG LOG END -------#
1040         return ($result);
1041 }
1042
1043 #!
1044 # Stop sslproxy process.
1045 #
1046 # @retval       OK/NG   start result
1047 #
1048 sub do_stop
1049 {
1050         #--------- DEBUG LOG ---------#
1051         &print_log("file", "DEBUG", "control", "10010008", 
1052                    "in_function : do_stop");
1053         #------- DEBUG LOG END -------#
1054
1055         my $result = $NG;
1056         my $stop_cnt = 0;
1057
1058         # Search stop target in sslproxyadm_data.
1059         foreach my $target (sort keys(%sslproxyadm_data)) {
1060                 #--------- DEBUG LOG ---------#
1061                 &print_log("file", "DEBUG", "control", "10010009", 
1062                            "Search stop target. ".
1063                            "target = $target");
1064                 #------- DEBUG LOG END -------#
1065                 # Check target section. (other is skip)
1066                 next if ($target !~ /^$TARGET_SECTION_KEY$/);
1067
1068                 # Stop executing, when target status Starting.
1069                 if ($sslproxyadm_data{$target}{"is_start"} == $OK) {
1070                         $stop_cnt++;
1071                         # Stop one target process.
1072                         ($result) = &stop_process($target);
1073                         if ($result == $NG) {
1074                                 &print_log("file", "ERROR", "control", "40010003", 
1075                                            "Stop process error. ".
1076                                            "target = $target");
1077                                 last;
1078                         }
1079                         #--------- DEBUG LOG ---------#
1080                         &print_log("file", "DEBUG", "control", "10010010", 
1081                                    "Stop target END. ".
1082                                    "target = $target");
1083                         #------- DEBUG LOG END -------#
1084                 } else {
1085                         &print_log("file", "WARN", "control", "30010003", 
1086                                    "Process already stopped. ".
1087                                    "target = $target");
1088                 }
1089         }
1090
1091         # Check stop target count.
1092         if ($stop_cnt == 0) {
1093                 &print_log("file", "WARN", "control", "30010004", 
1094                            "Target process not found for stop.");
1095         }
1096
1097         #--------- DEBUG LOG ---------#
1098         &print_log("file", "DEBUG", "control", "10010011", 
1099                    "out_function : do_stop : result = $result");
1100         #------- DEBUG LOG END -------#
1101         return ($result);
1102 }
1103
1104 #!
1105 # Restart sslproxy process.
1106 #
1107 # @retval       OK/NG   start result
1108 #
1109 sub do_restart
1110 {
1111         #--------- DEBUG LOG ---------#
1112         &print_log("file", "DEBUG", "control", "10010012", 
1113                    "in_function : do_restart");
1114         #------- DEBUG LOG END -------#
1115
1116         my $result = $NG;
1117         my $stop_cnt = 0;
1118         my $start_cnt = 0;
1119
1120         # Search restart target in sslproxyadm_data.
1121         foreach my $target (sort keys(%sslproxyadm_data)) {
1122                 #--------- DEBUG LOG ---------#
1123                 &print_log("file", "DEBUG", "control", "10010013", 
1124                            "Search restart target. ".
1125                            "target = $target");
1126                 #------- DEBUG LOG END -------#
1127                 # Check target section. (other is skip)
1128                 next if ($target !~ /^$TARGET_SECTION_KEY$/);
1129
1130                 # Stop executing for restart, when target status Starting.
1131                 if ($sslproxyadm_data{$target}{"is_start"} == $OK) {
1132                         $stop_cnt++;
1133                         # Stop one target process.
1134                         ($result) = &stop_process($target);
1135                         if ($result == $NG) {
1136                                 &print_log("file", "ERROR", "control", "40010004", 
1137                                            "Stop process for restart error. ".
1138                                            "target = $target");
1139                                 last;
1140                         }
1141                         #--------- DEBUG LOG ---------#
1142                         &print_log("file", "DEBUG", "control", "10010014", 
1143                                    "Stop target for restart END. ".
1144                                    "target = $target");
1145                         #------- DEBUG LOG END -------#
1146                 } else {
1147                         &print_log("file", "WARN", "control", "30010005", 
1148                                    "Process already stopped for restart. ".
1149                                    "target = $target");
1150                 }
1151
1152                 # Start executing for restart, when target status Stopped.
1153                 if ($sslproxyadm_data{$target}{"is_start"} == $NG) {
1154                         $start_cnt++;
1155                         # Start one target process.
1156                         ($result) = &start_process($target);
1157                         if ($result == $NG) {
1158                                 &print_log("file", "ERROR", "control", "40010005", 
1159                                            "Start process for restart error. ".
1160                                            "target = $target");
1161                                 last;
1162                         }
1163                         #--------- DEBUG LOG ---------#
1164                         &print_log("file", "DEBUG", "control", "10010015", 
1165                                    "Start target for restart END. ".
1166                                    "target = $target");
1167                         #------- DEBUG LOG END -------#
1168                 } else {
1169                         &print_log("file", "WARN", "control", "30010006", 
1170                                    "Process already starting for restart. ".
1171                                    "target = $target");
1172                 }
1173         }
1174
1175         # Check stop target for restart count.
1176         if ($stop_cnt == 0) {
1177                 &print_log("file", "WARN", "control", "30010007", 
1178                            "Target process not found for stop (restart).");
1179         }
1180
1181         # Check start target for restart count.
1182         if ($start_cnt == 0) {
1183                 &print_log("file", "WARN", "control", "30010008", 
1184                            "Target process not found for start (restart).");
1185         # When process start for restart failed, already Starting process will be Stopped.
1186         } elsif ($result == $NG) {
1187                 #--------- DEBUG LOG ---------#
1188                 &print_log("file", "DEBUG", "control", "10010016", 
1189                            "Start recover target for restart.");
1190                 #------- DEBUG LOG END -------#
1191
1192                 # Search recover target in sslproxyadm_data.
1193                 foreach my $recover_target (sort keys(%sslproxyadm_data)) {
1194                         #--------- DEBUG LOG ---------#
1195                         &print_log("file", "DEBUG", "control", "10010017", 
1196                                    "Search recover target for restart. ".
1197                                    "recover_target = $recover_target");
1198                         #------- DEBUG LOG END -------#
1199                         # Check target section. (other is skip)
1200                         next if ($recover_target !~ /^$TARGET_SECTION_KEY$/);
1201
1202                         # "now_start" flag is "ON" is Stopped. 
1203                         if ($sslproxyadm_data{$recover_target}{"now_start"} == $ON) {
1204                                 my $recover_result = $NG;
1205                                 # Recover(stop) one target process.
1206                                 ($recover_result) = &stop_process($recover_target);
1207                                 if ($recover_result == $NG) {
1208                                         &print_log("file", "ERROR", "control", "40010006", 
1209                                                    "Recover process  for restart error. ".
1210                                                    "recover_target = $recover_target");
1211                                 }
1212                                 #--------- DEBUG LOG ---------#
1213                                 &print_log("file", "DEBUG", "control", "10010018", 
1214                                            "Recover target  for restart END. ".
1215                                            "recover_target = $recover_target");
1216                                 #------- DEBUG LOG END -------#
1217                         }
1218                 }
1219         }
1220
1221         #--------- DEBUG LOG ---------#
1222         &print_log("file", "DEBUG", "control", "10010019", 
1223                    "out_function : do_restart : result = $result");
1224         #------- DEBUG LOG END -------#
1225         return ($result);
1226 }
1227
1228 #!
1229 # Reload sslproxy process.
1230 #
1231 # @retval       OK/NG   start result
1232 #
1233 sub do_reload
1234 {
1235         #--------- DEBUG LOG ---------#
1236         &print_log("file", "DEBUG", "control", "10010020", 
1237                    "in_function : do_reload");
1238         #------- DEBUG LOG END -------#
1239
1240         my $result = $NG;
1241         my $stop_cnt = 0;
1242         my $start_cnt = 0;
1243
1244         # Search reload target in sslproxyadm_data.
1245         foreach my $target (sort keys(%sslproxyadm_data)) {
1246                 #--------- DEBUG LOG ---------#
1247                 &print_log("file", "DEBUG", "control", "10010021", 
1248                            "Search reload target. ".
1249                            "target = $target");
1250                 #------- DEBUG LOG END -------#
1251                 # Check target section. (other is skip)
1252                 next if ($target !~ /^$TARGET_SECTION_KEY$/);
1253
1254                 # Stop executing for reload, when target status Starting
1255                 # and process status Abnormal(NG).
1256                 if ($sslproxyadm_data{$target}{"is_start"} == $OK) {
1257                         if ($sslproxyadm_data{$target}{"proc_status"} != $OK) {
1258                                 $stop_cnt++;
1259                                 # Stop one target process.
1260                                 ($result) = &stop_process($target);
1261                                 if ($result == $NG) {
1262                                         &print_log("file", "ERROR", "control", "40010007", 
1263                                                    "Stop process for reload error. ".
1264                                                    "target = $target");
1265                                         last;
1266                                 }
1267                                 #--------- DEBUG LOG ---------#
1268                                 &print_log("file", "DEBUG", "control", "10010022", 
1269                                            "Stop target for reload END. ".
1270                                            "target = $target");
1271                                 #------- DEBUG LOG END -------#
1272                         } else {
1273                                 #--------- DEBUG LOG ---------#
1274                                 &print_log("file", "DEBUG", "control", "10010023", 
1275                                            "Process normal starting for reload. ".
1276                                            "target = $target");
1277                                 #------- DEBUG LOG END -------#
1278                         }
1279                 } else {
1280                         &print_log("file", "WARN", "control", "30010009", 
1281                                    "Process already stopped for reload. ".
1282                                    "target = $target");
1283                 }
1284
1285                 # Start executing for reload, when target status Stopped.
1286                 if ($sslproxyadm_data{$target}{"is_start"} == $NG) {
1287                         $start_cnt++;
1288                         # Start one target process.
1289                         ($result) = &start_process($target);
1290                         if ($result == $NG) {
1291                                 &print_log("file", "ERROR", "control", "40010008", 
1292                                            "Start process for reload error. ".
1293                                            "target = $target");
1294                                 last;
1295                         }
1296                         #--------- DEBUG LOG ---------#
1297                         &print_log("file", "DEBUG", "control", "10010024", 
1298                                    "Start target for reload END. ".
1299                                    "target = $target");
1300                         #------- DEBUG LOG END -------#
1301                 } else {
1302                         &print_log("file", "WARN", "control", "30010010", 
1303                                    "Process already starting for reload. ".
1304                                    "target = $target");
1305                 }
1306         }
1307
1308         # Check stop target for reload count.
1309         if ($stop_cnt == 0) {
1310                 &print_log("file", "WARN", "control", "30010011", 
1311                            "Target process not found for stop (reload).");
1312         }
1313
1314         # Check start target for reload count.
1315         if ($start_cnt == 0) {
1316                 &print_log("file", "WARN", "control", "30010012", 
1317                            "Target process not found for start (reload).");
1318         # When process start for reload failed, already Starting process will be Stopped.
1319         } elsif ($result == $NG) {
1320                 #--------- DEBUG LOG ---------#
1321                 &print_log("file", "DEBUG", "control", "10010025", 
1322                            "Start recover target for reload.");
1323                 #------- DEBUG LOG END -------#
1324
1325                 # Search recover target in sslproxyadm_data.
1326                 foreach my $recover_target (sort keys(%sslproxyadm_data)) {
1327                         #--------- DEBUG LOG ---------#
1328                         &print_log("file", "DEBUG", "control", "10010026", 
1329                                    "Search recover target for reload. ".
1330                                    "recover_target = $recover_target");
1331                         #------- DEBUG LOG END -------#
1332                         # Check target section. (other is skip)
1333                         next if ($recover_target !~ /^$TARGET_SECTION_KEY$/);
1334
1335                         # "now_start" flag is "ON" is Stopped. 
1336                         if ($sslproxyadm_data{$recover_target}{"now_start"} == $ON) {
1337                                 my $recover_result = $NG;
1338                                 # Recover(stop) one target process.
1339                                 ($recover_result) = &stop_process($recover_target);
1340                                 if ($recover_result == $NG) {
1341                                         &print_log("file", "ERROR", "control", "40010009", 
1342                                                    "Recover process  for reload error. ".
1343                                                    "recover_target = $recover_target");
1344                                 }
1345                                 #--------- DEBUG LOG ---------#
1346                                 &print_log("file", "DEBUG", "control", "10010027", 
1347                                            "Recover target  for reload END. ".
1348                                            "recover_target = $recover_target");
1349                                 #------- DEBUG LOG END -------#
1350                         }
1351                 }
1352         }
1353
1354         #--------- DEBUG LOG ---------#
1355         &print_log("file", "DEBUG", "control", "10010028", 
1356                    "out_function : do_reload : result = $result");
1357         #------- DEBUG LOG END -------#
1358         return ($result);
1359 }
1360
1361 #!
1362 # Check all target is normal Starting.
1363 #
1364 # @retval       OK/NG   check result
1365 #
1366 sub do_check
1367 {
1368         #--------- DEBUG LOG ---------#
1369         &print_log("file", "DEBUG", "status", "10020030", 
1370                    "in_function : do_check");
1371         #------- DEBUG LOG END -------#
1372
1373         my $result = $OK;
1374
1375         # Check all target in sslproxyadm_data.
1376         foreach my $target (sort keys(%sslproxyadm_data)) {
1377                 # Get target id from sslproxyadm_data
1378                 # Check target section. (other is skip)
1379                 next if ($target !~ /^$TARGET_SECTION_KEY$/);
1380                 #--------- DEBUG LOG ---------#
1381                 &print_log("file", "DEBUG", "status", "10020031", 
1382                            "Check target = $target");
1383                 #------- DEBUG LOG END -------#
1384
1385                 # Check proc_status is OK and prosess is Starting.
1386                 if ($sslproxyadm_data{$target}{"proc_status"} != $OK ||
1387                     $sslproxyadm_data{$target}{"is_start"} != $OK) {
1388                         &print_log("file", "WARN", "status", "30020001", 
1389                                    "Check NG. Found NG target. target = $target");
1390                         $result = $NG;
1391                         last;
1392                 }
1393         }
1394
1395         #--------- DEBUG LOG ---------#
1396         &print_log("file", "DEBUG", "status", "10020032", 
1397                    "out_function : do_check : result = $result");
1398         #------- DEBUG LOG END -------#
1399         return ($result);
1400 }
1401
1402 #!
1403 # Print all status.
1404 # Logger, Target, Process.
1405 #
1406 # @retval       OK/NG   print result (always OK)
1407 #
1408 sub do_status
1409 {
1410         #--------- DEBUG LOG ---------#
1411         &print_log("file", "DEBUG", "status", "10020033", 
1412                    "in_function : do_status");
1413         #------- DEBUG LOG END -------#
1414
1415         my $result = $OK;
1416
1417         print STDOUT "----- Print SSLproxyadm status start -----\n";
1418
1419         # Print common information.
1420         print STDOUT "[ Common Data ]\n";
1421         print STDOUT "Common config file        : $CONFIG_FILE\n";
1422         print STDOUT "Output log file           : $LOG_FILE\n";
1423         print STDOUT "Output log level  : $LOG_LEVEL\n";
1424         print STDOUT "Log rotate config file    : $LOGROTATE_CONF_FILE\n";
1425         print STDOUT "Log rotate status file    : $LOGROTATE_STAT_FILE\n";
1426         print STDOUT "\n";
1427
1428         # Print all target information in sslproxyadm_data.
1429         print STDOUT "[ Target Data ]\n";
1430         my $target_data_count = keys(%sslproxyadm_data);
1431         if ($target_data_count == 0) {
1432                 print STDOUT "  Target nothing.\n";
1433         } else {
1434                 foreach my $target (sort keys(%sslproxyadm_data)) {
1435                         # Get target id from sslproxyadm_data
1436                         # Check target section. (other is skip)
1437                         next if ($target !~ /^$TARGET_SECTION_KEY$/);
1438
1439                         print STDOUT "TargetID : $target\n";
1440                         print STDOUT "  Config file     : " . 
1441                                      $sslproxyadm_data{$target}{"conf_file"} . 
1442                                      "\n";
1443
1444                         if ($sslproxyadm_data{$target}{"proc_status"} == $OK) {
1445                                 print STDOUT "  Process status  : Normal.\n";
1446                         } else {
1447                                 print STDOUT "  Process status  : Abnormal.\n";
1448                         }
1449
1450                         if ($sslproxyadm_data{$target}{"is_start"} == $OK) {
1451                                 print STDOUT "  Execute status  : Starting. " . 
1452                                              "PID = " . 
1453                                              $sslproxyadm_data{$target}{"pid"} . 
1454                                              "\n";
1455                         } else {
1456                                 print STDOUT "  Execute status  : Stopped.\n";
1457                         }
1458
1459                         if ($sslproxyadm_data{$target}{"proc_status"} == $NG) {
1460                                 print STDOUT "  Status detail   : \n";
1461                                 if ($sslproxyadm_data{$target}{"is_start"} == $OK) {
1462                                         if ($sslproxyadm_data{$target}{"cmp_config"} == $NG) {
1463                                                 print STDOUT "                  ".
1464                                                 ": Different configuration file is used now.\n";
1465                                         }
1466                                         if ($sslproxyadm_data{$target}{"start_info"} == $NG) {
1467                                                 print STDOUT "                  ".
1468                                                 ": Start information file nothing. (Manual start?)\n";
1469                                         } else {
1470                                                 print STDOUT "                  ".
1471                                                 ": Start information file exist. (Change config?)\n";
1472                                                 if ($sslproxyadm_data{$target}{"cmp_start_info"} == $NG) {
1473                                                         print STDOUT "                  ".
1474                                                         ": Start information file is Different. \n";
1475                                                 }
1476                                         }
1477                                 } else {
1478                                         if ($sslproxyadm_data{$target}{"start_info"} == $OK) {
1479                                                 print STDOUT "                  ".
1480                                                 ": Start information file exist. (Change config or Manual stop?)\n";
1481                                                 if ($sslproxyadm_data{$target}{"cmp_start_info"} == $NG) {
1482                                                         print STDOUT "                  ".
1483                                                         ": Start information file is Different. \n";
1484                                                 }
1485                                         }
1486                                 }
1487                         }
1488                 }
1489         }
1490         print STDOUT "\n";
1491
1492         # Print unknown process information in process_data.
1493         print STDOUT "[ Unknown SSLproxy process ]\n";
1494         my $process_data_count = keys(%process_data);
1495         if ($process_data_count == 0) {
1496                 print STDOUT "  Process nothing.\n";
1497         } else {
1498                 foreach my $proc_target (sort keys(%process_data)) {
1499                         if ($process_data{$proc_target}{"known"} != $ON) {
1500                                 print STDOUT "TargetID : $proc_target\n";
1501                                 print STDOUT "  Config file     : " . 
1502                                              $process_data{$proc_target}{"conf_file"} . 
1503                                              "\n";
1504                                 print STDOUT "  PID             : " . 
1505                                              $process_data{$proc_target}{"pid"} . 
1506                                              "\n";
1507                         }
1508                 }
1509         }
1510
1511         print STDOUT "----- Print SSLproxyadm status end   -----\n";
1512
1513         #--------- DEBUG LOG ---------#
1514         &print_log("file", "DEBUG", "status", "10020034", 
1515                    "out_function : do_status : result = $result");
1516         #------- DEBUG LOG END -------#
1517         return ($result);
1518 }
1519
1520 #!
1521 # Check target configuration file format.
1522 #
1523 # @retval       OK/NG   check result
1524 #
1525 sub do_config
1526 {
1527         #--------- DEBUG LOG ---------#
1528         &print_log("file", "DEBUG", "config", "10030017", 
1529                    "in_function : do_config");
1530         #------- DEBUG LOG END -------#
1531
1532         my $result = $NG;
1533
1534         # Search target configration file.
1535         foreach my $section (sort keys(%sslproxyadm_data)) {
1536                 #--------- DEBUG LOG ---------#
1537                 &print_log("file", "DEBUG", "config", "10030018", 
1538                            "Search section. ".
1539                            "section = $section");
1540                 #------- DEBUG LOG END -------#
1541                 my $target_conf_file = "";
1542                 # Check target section.
1543                 next if ($section !~ /^$TARGET_SECTION_KEY$/);
1544                 # Get target configration file name.
1545                 $target_conf_file = $sslproxyadm_data{$section}{"conf_file"};
1546                 # Read target configration file for format check.
1547                 %target_data = ();
1548                 ($result) = &read_config($target_conf_file, "target");
1549                 if ($result == $NG) {
1550                         &print_log("file", "ERROR", "config", "40030012", 
1551                                    "Target config file format error. ".
1552                                    "file = $target_conf_file");
1553                         last;
1554                 }
1555                 #--------- DEBUG LOG ---------#
1556                 &print_log("file", "DEBUG", "config", "10030019", 
1557                            "Target config file format OK. ".
1558                            "target_conf_file = $target_conf_file");
1559                 #------- DEBUG LOG END -------#
1560
1561                 # for debug.
1562                 #&print_table_data(%target_data);
1563         }
1564
1565         #--------- DEBUG LOG ---------#
1566         &print_log("file", "DEBUG", "config", "10030020", 
1567                    "out_function : do_config : ".
1568                    "result = $result");
1569         #------- DEBUG LOG END -------#
1570         return ($result);
1571 }
1572
1573 #!
1574 # Start specified sslproxy process.
1575 # Start process and Create start information and Update process status.
1576 #
1577 # @param[in]    target_id       target id
1578 # @retval       OK/NG   start result
1579 #
1580 sub start_process
1581 {
1582         # Argment.
1583         my ($target_id) = (@_);
1584         my $result = $NG;
1585         #--------- DEBUG LOG ---------#
1586         &print_log("file", "DEBUG", "control", "10010029", 
1587                    "in_function : start_process : ".
1588                    "target_id = $target_id");
1589         #------- DEBUG LOG END -------#
1590
1591         my $config_file = $sslproxyadm_data{$target_id}{"conf_file"};
1592
1593         # Check sslproxy command file. Try open for exist check.
1594         if (open(TEMP, "<$SSLPROXY_PATH$SSLPROXY")) {
1595                 close(TEMP);
1596                 # Execute sslproxy start command.
1597                 # /usr/sbin/sslproxy target_id config_file
1598                 my $cmd_result = system("$SSLPROXY_PATH$SSLPROXY $target_id $config_file");
1599                 # Check command result.
1600                 if ($cmd_result != 0) {
1601                         &print_log("file", "ERROR", "control", "40010010", 
1602                                    "sslproxy command execution error.");
1603                 } else {
1604                         # Check Starting sslproxy exist or not.
1605                         # Open ps command output.
1606                         my $ps_cmd = "/bin/ps -C $SSLPROXY -o pid=,cmd= |";
1607                         if (!open(PROCESS, $ps_cmd)) {
1608                                 &print_log("file", "ERROR", "control", "40010011", 
1609                                            "Cannot open ps command output.");
1610                         } else {
1611                                 # Read ps command data.
1612                                 my $line_data = "";
1613                                 while ($line_data = <PROCESS>) {
1614                                         # Read one line data. (one process)
1615                                         # Cut \n.
1616                                         $line_data =~ s/^\s*|\n$//g;
1617                                         #--------- DEBUG LOG ---------#
1618                                         &print_log("file", "DEBUG", "control", "10010030", 
1619                                                    "Read one process line data = $line_data");
1620                                         #------- DEBUG LOG END -------#
1621
1622                                         # Split @param by " " delimiter.
1623                                         # $param[0] -> pid
1624                                         # $param[1] -> cmd (ex. "./sslproxy")
1625                                         # $param[2] -> target id
1626                                         # $param[3] -> target config file
1627                                         my @param = split /\s/, $line_data, 4;
1628                                         # Check target_id exist or not.
1629                                         if ($param[2] eq $target_id) {
1630                                                 # Target process found. Save pid.
1631                                                 $sslproxyadm_data{$target_id}{"pid"} = $param[0];
1632                                                 $result = $OK;
1633                                                 #--------- DEBUG LOG ---------#
1634                                                 &print_log("file", "DEBUG", "control", "10010031", 
1635                                                            "Starting target process found.");
1636                                                 #------- DEBUG LOG END -------#
1637                                                 last;
1638                                         }
1639                                 }
1640                                 close(PROCESS);
1641                         }
1642                 }
1643         } else {
1644                 &print_log("file", "ERROR", "control", "40010012", 
1645                            "SSLproxy command file not exist.");
1646         }
1647
1648         if ($result == $NG) {
1649                 &print_log("file", "ERROR", "control", "40010013", 
1650                            "Start target process error. ".
1651                            "target_id = $target_id");
1652         } else {
1653                 # Set and update process status. (Starting)
1654                 ($result) = &update_process_status($target_id, $ON);
1655                 if ($result == $NG) {
1656                         &print_log("file", "ERROR", "control", "40010014", 
1657                                    "Update target process status (Starting) error. ".
1658                                    "target_id = $target_id");
1659                         &stop_process($target_id);
1660                 }
1661                 #--------- DEBUG LOG ---------#
1662                 &print_log("file", "DEBUG", "control", "10010032", 
1663                            "Update target process status (Starting) END.");
1664                 #------- DEBUG LOG END -------#
1665         }
1666
1667         if ($result == $OK) {
1668                 &print_log("file", "INFO", "control", "20010001", 
1669                            "SSLproxy process start OK. ".
1670                            "target_id = $target_id");
1671         }
1672
1673         #--------- DEBUG LOG ---------#
1674         &print_log("file", "DEBUG", "control", "10010033", 
1675                    "out_function : start_process : ".
1676                    "result = $result");
1677         #------- DEBUG LOG END -------#
1678         return ($result);
1679 }
1680
1681 #!
1682 # Stop specified sslproxy process.
1683 # Stop process and Delete start information and Update process status.
1684 #
1685 # @param[in]    target_id       target id
1686 # @retval       OK/NG   start result
1687 #
1688 sub stop_process
1689 {
1690         # Argment.
1691         my ($target_id) = (@_);
1692         my $result = $NG;
1693         #--------- DEBUG LOG ---------#
1694         &print_log("file", "DEBUG", "control", "10010034", 
1695                    "in_function : stop_process : ".
1696                    "target_id = $target_id");
1697         #------- DEBUG LOG END -------#
1698
1699         my $pid = $sslproxyadm_data{$target_id}{"pid"};
1700
1701         # Execute sslproxy stop command.
1702         # /bin/kill sslproxy_pid
1703         system("/bin/kill $pid");
1704
1705         # Check Stopped sslproxy exist or not.
1706         # Open grep command output.
1707         my $grep_cmd = "/bin/ps -C $SSLPROXY -o args --no-headers | ".
1708                        "/bin/cut -d\" \" -f2 | ".
1709                        "/bin/grep $target_id |";
1710         if (!open(GREPDATA, $grep_cmd)) {
1711                 &print_log("file", "ERROR", "control", "40010015", 
1712                            "Cannot open shell command output.");
1713         } else {
1714                 #--------- DEBUG LOG ---------#
1715                 &print_log("file", "DEBUG", "control", "10010035", 
1716                            "Get grep command output.");
1717                 #------- DEBUG LOG END -------#
1718                 my @grep_data = <GREPDATA>;
1719                 my $grep_cnt = @grep_data;
1720                 # Check grep output size. 0 is target process not found.
1721                 if ($grep_cnt == 0) {
1722                         #--------- DEBUG LOG ---------#
1723                         &print_log("file", "DEBUG", "control", "10010036", 
1724                                    "Stopped target process not found (stop OK).");
1725                         #------- DEBUG LOG END -------#
1726                         $result = $OK;
1727                 }
1728                 close(GREPDATA);
1729         }
1730
1731         if ($result == $NG) {
1732                 &print_log("file", "ERROR", "control", "40010016", 
1733                            "Stop target process error. ".
1734                            "target_id = $target_id");
1735         } else {
1736                 # Set and update process status. (Stopped)
1737                 ($result) = &update_process_status($target_id, $OFF);
1738                 if ($result == $NG) {
1739                         &print_log("file", "ERROR", "control", "40010017", 
1740                                    "Update target process status (Stopped) error. ".
1741                                    "target_id = $target_id");
1742                 }
1743                 #--------- DEBUG LOG ---------#
1744                 &print_log("file", "DEBUG", "control", "10010037", 
1745                            "Update target process status (Stopped) END.");
1746                 #------- DEBUG LOG END -------#
1747         }
1748
1749         if ($result == $OK) {
1750                 &print_log("file", "INFO", "control", "20010002", 
1751                            "SSLproxy process stop OK. ".
1752                            "target_id = $target_id");
1753         }
1754
1755         #--------- DEBUG LOG ---------#
1756         &print_log("file", "DEBUG", "control", "10010038", 
1757                    "out_function : stop_process : ".
1758                    "result = $result");
1759         #------- DEBUG LOG END -------#
1760         return ($result);
1761 }
1762
1763 #!
1764 # Set logger.
1765 # Set logfile/loglevel and Execute logrotate.
1766 #
1767 # @retval       OK/NG   check result
1768 #
1769 sub set_log
1770 {
1771         #--------- DEBUG LOG ---------#
1772         &print_log("file", "DEBUG", "logger", "10040001", 
1773                    "in_function : set_log");
1774         #------- DEBUG LOG END -------#
1775         my $result = $OK;
1776
1777         #--------- DEBUG LOG ---------#
1778         &print_log("file", "DEBUG", "logger", "10040002", 
1779                    "Config before value. ".
1780                    "LOG_LEVEL = $LOG_LEVEL, ".
1781                    "LOG_FILE = $LOG_FILE");
1782         #------- DEBUG LOG END -------#
1783
1784         # Get loglevel from logger configuration.
1785         my $loglevel = $sslproxyadm_data{$LOGGER_SECTION_KEY}{"log_level"};
1786         if ($loglevel eq "") {
1787                 &print_log("file", "WARN", "logger", "30040001", 
1788                            "Log level not found in config. ".
1789                            "Use default log level. ".
1790                            "level = $LOG_LEVEL");
1791                 $loglevel = $LOG_LEVEL;
1792         } else {
1793                 # Check config log level range.
1794                 if ($loglevel ne "debug" &&
1795                     $loglevel ne "info"  &&
1796                     $loglevel ne "warn"  &&
1797                     $loglevel ne "error" &&
1798                     $loglevel ne "fatal") {
1799                         &print_log("file", "ERROR", "logger", "40040001", 
1800                                    "Invalid config log level. ".
1801                                    "level = $loglevel");
1802                         $result = $NG;
1803                 } else {
1804                         #--------- DEBUG LOG ---------#
1805                         &print_log("file", "DEBUG", "logger", "10040003", 
1806                                    "Config log level is $loglevel.");
1807                         #------- DEBUG LOG END -------#
1808                 }
1809         }
1810
1811         # Get logfile from logger configuration.
1812         my $logfile = $sslproxyadm_data{$LOGGER_SECTION_KEY}{"log_filename"};
1813         if ($result == $OK) {
1814                 if ($logfile eq "") {
1815                         &print_log("file", "WARN", "logger", "30040002", 
1816                                    "Log file not found in config. ".
1817                                    "Use default log file. ".
1818                                    "file = $LOG_FILE");
1819                         $logfile = $LOG_FILE;
1820                 } else {
1821                         # Check log file. Try open.
1822                         if(!open(LOGFILE, ">>$logfile")) {
1823                                 &print_log("file", "ERROR", "logger", "40040002", 
1824                                            "Cannot open config log file. ".
1825                                            "file = $logfile");
1826                                 $result = $NG;
1827                         } else {
1828                                 close(LOGFILE);
1829                                 if ($logfile ne $LOG_FILE) {
1830                                         &print_log("file", "WARN", "logger", "30040003", 
1831                                                    "Log file is chaged. ".
1832                                                    "$LOGROTATE_CONF_FILE file ".
1833                                                    "should also change for log rotate.");
1834                                 } else {
1835                                         #--------- DEBUG LOG ---------#
1836                                         &print_log("file", "DEBUG", "logger", "10040004", 
1837                                                    "Log file is still default.");
1838                                         #------- DEBUG LOG END -------#
1839                                 }
1840                         }
1841                 }
1842         }
1843
1844         # Execute logrotate command.
1845         if ($result == $OK) {
1846                 ($result) = system("$LOGROTATE_CMD $LOGROTATE_CONF_FILE");
1847                 if ($result != $OK) {
1848                         &print_log("file", "ERROR", "logger", "40040003", 
1849                                    "Logrotate execute error. : $!");
1850                         $result = $NG;
1851                         #--------- DEBUG LOG ---------#
1852                         &print_log("file", "DEBUG", "logger", "10040005", 
1853                                    "Not change logger setteing.");
1854                         #------- DEBUG LOG END -------#
1855                 } else {
1856                         # Change logger setteing.
1857                         $LOG_LEVEL = $loglevel;
1858                         $LOG_FILE = $logfile;
1859                         #--------- DEBUG LOG ---------#
1860                         &print_log("file", "DEBUG", "logger", "10040006", 
1861                                    "Change logger setteing. ".
1862                                    "level = $LOG_LEVEL, ".
1863                                    "file = $LOG_FILE");
1864                         #------- DEBUG LOG END -------#
1865                 }
1866         }
1867
1868         #--------- DEBUG LOG ---------#
1869         &print_log("file", "DEBUG", "logger", "10040007", 
1870                    "out_function : set_log : ".
1871                    "result = $result");
1872         #------- DEBUG LOG END -------#
1873         return ($result);
1874 }
1875
1876 #!
1877 # Output log message to console or log file.
1878 #
1879 # output device string  -> cout/cerr/file/fcout/fcerr
1880 # log level string      -> DEBUG/INFO/WARN/ERROR/FATAL (1-5)
1881 # log category string   -> control/status/config/logger/common (1-5)
1882 #                          (sslproxyadm_xxxxx)
1883 # message number        -> level_numberX + category_numberXXX + XXXX
1884 #                          (SLAXXXXXXXX)
1885 #
1886 # @param[in]    outdev          output device
1887 # @param[in]    level           message log level
1888 # @param[in]    category        log category
1889 # @param[in]    msgid           message number
1890 # @param[in]    msg             message string
1891 #
1892 sub print_log
1893 {
1894         # Argment.
1895         my ($outdev, $level, $category, $msgid, $msg) = (@_);
1896         my $result = $NG;
1897         #--------- DEBUG LOG ---------#
1898         if ($LOGGER_DEBUG) {
1899                 print STDOUT "in_function : print_log : ".
1900                              "outdev = $outdev, ".
1901                              "level = $level, ".
1902                              "category = $category, ".
1903                              "msgid = $msgid, ".
1904                              "msg = $msg\n";
1905         }
1906         #------- DEBUG LOG END -------#
1907
1908         # Check message and output log level.
1909         ($result) =&check_log_level($level);
1910         if ($result == $OK) {
1911                 # Check log category range.
1912                 if ($category ne "control" &&
1913                     $category ne "status" &&
1914                     $category ne "config" &&
1915                     $category ne "logger" &&
1916                     $category ne "common") {
1917                         print STDERR "Print log error : Invalid log category.\n";
1918                 # Check log output device range.
1919                 } elsif ($outdev ne "cout" &&
1920                     $outdev ne "cerr" &&
1921                     $outdev ne "file" &&
1922                     $outdev ne "fcout" &&
1923                     $outdev ne "fcerr") {
1924                         print STDERR "Print log error : Invalid log output device.\n";
1925                 } else {
1926                         # Print to console.
1927                         if ($outdev eq "cout" || $outdev eq "fcout") {
1928                                 print STDOUT "$msg\n";
1929                         } elsif ($outdev eq "cerr" || $outdev eq "fcerr") {
1930                                 print STDERR "$msg\n";
1931                         }
1932                         # Print to lof file.
1933                         if ($outdev eq "file" || $outdev eq "fcout" || $outdev eq "fcerr") {
1934                                 if(!open(LOGFILE, ">>$LOG_FILE")) {
1935                                         print STDERR "Cannot open log file.\n";
1936                                 } else {
1937                                         my $now_date = localtime();
1938                                         print LOGFILE "$now_date ".
1939                                                       "[$level] ".
1940                                                       "sslproxyadm_$category ".
1941                                                       "SLA$msgid ".
1942                                                       "$msg\n";
1943                                         close(LOGFILE);
1944                                 }
1945                         }
1946                 }
1947         }
1948
1949         #--------- DEBUG LOG ---------#
1950         if ($LOGGER_DEBUG) {
1951                 print STDOUT "out_function : print_log\n";
1952         }
1953         #------- DEBUG LOG END -------#
1954 }
1955
1956 #!
1957 # Check message and output log level.
1958 #
1959 # @param[in]    level   message log level
1960 # @retval       OK/NG   check result
1961 #
1962 sub check_log_level
1963 {
1964         # Argment.
1965         my ($level) = (@_);
1966         my $result = $NG;
1967         #--------- DEBUG LOG ---------#
1968         if ($LOGGER_DEBUG) {
1969                 print STDOUT "in_function : check_log_level : level = $level\n";
1970         }
1971         #------- DEBUG LOG END -------#
1972
1973         # Check message log level range.
1974         if ($level ne "DEBUG" &&
1975             $level ne "INFO"  &&
1976             $level ne "WARN"  &&
1977             $level ne "ERROR" &&
1978             $level ne "FATAL") {
1979                 print STDERR "Print log error : Invalid message log level.\n";
1980         # Check output log level.
1981         } elsif ($LOG_LEVEL eq "debug") {
1982                 $result = $OK;
1983                 #--------- DEBUG LOG ---------#
1984                 if ($LOGGER_DEBUG) {
1985                         print STDOUT "Logout level OK. out[$LOG_LEVEL] msg[$level]\n";
1986                 }
1987                 #------- DEBUG LOG END -------#
1988         } elsif ($LOG_LEVEL eq "info") {
1989                 if ($level eq "INFO"  ||
1990                     $level eq "WARN"  ||
1991                     $level eq "ERROR" ||
1992                     $level eq "FATAL") {
1993                         $result = $OK;
1994                         #--------- DEBUG LOG ---------#
1995                         if ($LOGGER_DEBUG) {
1996                                 print STDOUT "Logout level OK. out[$LOG_LEVEL] msg[$level]\n";
1997                         }
1998                         #------- DEBUG LOG END -------#
1999                 } else {
2000                         #--------- DEBUG LOG ---------#
2001                         if ($LOGGER_DEBUG) {
2002                                 print STDOUT "Logout level NG. out[$LOG_LEVEL] msg[$level]\n";
2003                         }
2004                         #------- DEBUG LOG END -------#
2005                 }
2006         } elsif ($LOG_LEVEL eq "warn") {
2007                 if ($level eq "WARN"  ||
2008                     $level eq "ERROR" ||
2009                     $level eq "FATAL") {
2010                         $result = $OK;
2011                         #--------- DEBUG LOG ---------#
2012                         if ($LOGGER_DEBUG) {
2013                                 print STDOUT "Logout level OK. out[$LOG_LEVEL] msg[$level]\n";
2014                         }
2015                         #------- DEBUG LOG END -------#
2016                 } else {
2017                         #--------- DEBUG LOG ---------#
2018                         if ($LOGGER_DEBUG) {
2019                                 print STDOUT "Logout level NG. out[$LOG_LEVEL] msg[$level]\n";
2020                         }
2021                         #------- DEBUG LOG END -------#
2022                 }
2023         } elsif ($LOG_LEVEL eq "error") {
2024                 if ($level eq "ERROR"  ||
2025                     $level eq "FATAL") {
2026                         $result = $OK;
2027                         #--------- DEBUG LOG ---------#
2028                         if ($LOGGER_DEBUG) {
2029                                 print STDOUT "Logout level OK. out[$LOG_LEVEL] msg[$level]\n";
2030                         }
2031                         #------- DEBUG LOG END -------#
2032                 } else {
2033                         #--------- DEBUG LOG ---------#
2034                         if ($LOGGER_DEBUG) {
2035                                 print STDOUT "Logout level NG. out[$LOG_LEVEL] msg[$level]\n";
2036                         }
2037                         #------- DEBUG LOG END -------#
2038                 }
2039         } elsif ($LOG_LEVEL eq "fatal") {
2040                 if ($level eq "FATAL") {
2041                         $result = $OK;
2042                         #--------- DEBUG LOG ---------#
2043                         if ($LOGGER_DEBUG) {
2044                                 print STDOUT "Logout level OK. out[$LOG_LEVEL] msg[$level]\n";
2045                         }
2046                         #------- DEBUG LOG END -------#
2047                 } else {
2048                         #--------- DEBUG LOG ---------#
2049                         if ($LOGGER_DEBUG) {
2050                                 print STDOUT "Logout level NG. out[$LOG_LEVEL] msg[$level]\n";
2051                         }
2052                         #------- DEBUG LOG END -------#
2053                 }
2054         } else {
2055                 print STDERR "Print log error : Invalid output log level.\n";
2056         }
2057
2058         #--------- DEBUG LOG ---------#
2059         if ($LOGGER_DEBUG) {
2060                 print STDOUT "out_function : check_log_level : result = $result\n";
2061         }
2062         #------- DEBUG LOG END -------#
2063         return ($result);
2064 }
2065
2066 #!
2067 # Show command usage.
2068 #
2069 sub usage
2070 {
2071         #--------- DEBUG LOG ---------#
2072         &print_log("file", "DEBUG", "common", "10050028", 
2073                    "in_function : usage");
2074         #------- DEBUG LOG END -------#
2075
2076         print STDERR "\nUsage : sslproxyadm ".
2077                      "\{start|stop|restart|reload|check|status|config\} ".
2078                      "[config_file_name]\n".
2079                      "  start           Start SSLproxy process.\n".
2080                      "  stop            Stop SSLproxy process.\n".
2081                      "  restart Restart SSLproxy process.\n".
2082                      "  reload  Re-configure SSLproxy process.\n".
2083                      "  check           Check SSLproxy process status.\n".
2084                      "  status  Print SSLproxy process status.\n".
2085                      "  config  Check SSLproxy configration file.\n\n";
2086
2087         #--------- DEBUG LOG ---------#
2088         &print_log("file", "DEBUG", "common", "10050029", 
2089                    "out_function : usage");
2090         #------- DEBUG LOG END -------#
2091 }
2092
2093 #!
2094 # Exit sslproxyadm command.
2095 #
2096 # @param[in]    result  command result
2097 #
2098 sub do_exit
2099 {
2100         # Argment.
2101         my ($result) = (@_);
2102         #--------- DEBUG LOG ---------#
2103         &print_log("file", "DEBUG", "common", "10050030", 
2104                    "in_function : do_exit : result = $result");
2105         #------- DEBUG LOG END -------#
2106
2107         if ($result == $OK) {
2108                 &print_log("fcout", "INFO", "common", "20050001", 
2109                            "SSLproxyadm command ($CMD_OPT) is successful.");
2110         } elsif ($result == $NG) {
2111                 &print_log("fcerr", "INFO", "common", "20050002", 
2112                            "SSLproxyadm command ($CMD_OPT) is failure.");
2113         }
2114
2115         #--------- DEBUG LOG ---------#
2116         &print_log("file", "DEBUG", "common", "10050031", 
2117                    "out_function : do_exit");
2118         #------- DEBUG LOG END -------#
2119         exit($result);
2120 }
2121
2122 #!
2123 # Print specified table data to STDOUT for debug.
2124 #
2125 # @param[in]    table_data      command result
2126 #
2127 sub print_table_data
2128 {
2129         my (%table_data) = (@_);
2130         print STDOUT "\n*** Print table data ***\n";
2131         my $section_count = keys(%table_data);
2132         print STDOUT "Section count[$section_count]\n";
2133         while (my ($section_name, $section_data) = each (%table_data)) {
2134                 my $key_count = keys(%$section_data);
2135                 print STDOUT "  [$section_name] section. Key count[$key_count]\n";
2136                 while (my ($key, $value) = each (%$section_data)) {
2137                         print STDOUT "          Key[$key] Value[$value]\n";
2138                 }
2139         }
2140         print STDOUT "\n";
2141 }