OSDN Git Service

Initial commit
[ultramonkey-l7/l7gui.git] / src / src / org / ultramonkey / l7 / controller / L7vsAdmController.java
1 package org.ultramonkey.l7.controller;
2
3 import org.apache.log4j.Logger;
4 import org.ultramonkey.l7.model.*;
5 import java.util.regex.*;
6 import java.util.*;
7 import java.io.*;
8
9 /**
10  * 
11  * <p>
12  * class L7vsAdmController
13  * </p>
14  * <p>
15  * Copyright(c) NTT COMWARE 2008
16  * </p>
17  * 
18  * @author tanuma
19  */
20 public class L7vsAdmController {
21         private static final String l7vsadm = "/usr/sbin/l7vsadm";
22
23         private Logger ioCommandLogger = Logger.getLogger(LogCategorySet.GUI_IO_COMMAND);
24         
25         /**
26          * 
27          * <p>
28          * addVirtualService method
29          * </p>
30          * 
31          * @param vs
32          * @return result of l7vsadm add virtual service command.
33          */
34         public boolean addVirtualService(VirtualSetting vs) {
35         // --- debug log (in method) ---
36         if (ioCommandLogger.isDebugEnabled()) {
37             StringBuffer buf = new StringBuffer();
38             buf.append("L7vsAdmController::addVirtualService(VirtualSetting vs) in ");
39             buf.append("vs=(" + vs + ")");
40             ioCommandLogger.debug("11524 " + buf.toString());
41         }
42         // --- debug log (in method) ---
43                 
44                 synchronized (this) {
45                         if (vs == null || vs.virtual == null) {
46                                 ioCommandLogger.warn("31003 Invalid null argument.");
47                         // --- debug log (in method) ---
48                         if (ioCommandLogger.isDebugEnabled()) {
49                             ioCommandLogger.debug("11525 L7vsAdmController::addVirtualService(VirtualSetting vs) out return=false");
50                         }
51                         // --- debug log (in method) ---
52                                 return false;
53                         }
54                         
55                         StringBuffer command = new StringBuffer(l7vsadm + " -A -t "
56                                         + vs.virtual.host + ":" + vs.virtual.port + " -m "
57                                         + vs.protomod + " " + vs.option + " -s " + vs.sched);
58                         if (vs.maxconn > 0) {
59                                 command.append(" -u " + vs.maxconn);
60                         }
61                         if (vs.sorryserver != null && vs.sorryserver.host.length() > 0 && vs.sorryserver.port.length() > 0) {
62                                 command.append(" -b " + vs.sorryserver.host + ":"
63                                                 + vs.sorryserver.port);
64                         }
65                         if (vs.sorryflag == true) {
66                                 command.append(" -f 1");
67                         }
68                         if (vs.qosservice > 0) {
69                                 if (vs.qosservice >= 1000000000L) {
70                                         long qoss = vs.qosservice / 1000000000L;
71                                         command.append(" -Q " + qoss + "G");
72                                 }
73                                 else if (vs.qosservice >= 1000000L) {
74                                         long qoss = vs.qosservice / 1000000L;
75                                         command.append(" -Q " + qoss + "M");
76                                 }
77                                 else if (vs.qosservice >= 1000L) {
78                                         long qoss = vs.qosservice / 1000L;
79                                         command.append(" -Q " + qoss + "K");
80                                 }
81                                 else {
82                                         command.append(" -Q " + vs.qosservice);
83                                 }
84                         }
85                         if (vs.qosclient > 0) {
86                                 if (vs.qosclient >= 1000000000L) {
87                                         long qosc = vs.qosclient / 1000000000L;
88                                         command.append(" -q " + qosc + "G");
89                                 }
90                                 else if (vs.qosclient >= 1000000L) {
91                                         long qosc = vs.qosclient / 1000000L;
92                                         command.append(" -q " + qosc + "M");
93                                 }
94                                 else if (vs.qosclient >= 1000L) {
95                                         long qosc = vs.qosclient / 1000L;
96                                         command.append(" -q " + qosc + "K");
97                                 }
98                                 else {
99                                         command.append(" -q " + vs.qosclient);
100                                 }
101                         }
102         
103                         String result = runProcess(command.toString());
104                         if (result == null || result.length() != 0) {
105                                 ioCommandLogger.error("41220 Command error: command=" + command);
106                                 ioCommandLogger.error("41221 Command error:  result=" + result);
107                                 // --- debug log (out method) ---
108                                 if (ioCommandLogger.isDebugEnabled()) {
109                             ioCommandLogger.debug("11526 L7vsAdmController::addVirtualService(VirtualSetting vs) out return=false");
110                                 }
111                                 // --- debug log (out method) ---
112                                 return false;
113                         }
114                         
115                         if (vs.real != null) {
116                                 for (int i = 0; i < vs.real.size(); i++) {
117                                         EndPoint real = vs.real.elementAt(i);
118                                         String realCommand = l7vsadm + " -a -t " + vs.virtual.host + ":" + vs.virtual.port + " -m "
119                                                 + vs.protomod + " " + vs.option + " -r " + real.host + ":" + real.port + " -w " + real.weight;
120                                         result = runProcess(realCommand);
121                                         
122                                         if (result == null || result.length() != 0) {
123                                                 ioCommandLogger.error("41222 Command error: command=" + command);
124                                                 ioCommandLogger.error("41223 Command error:  result=" + result);
125                                                 // --- debug log (out method) ---
126                                                 if (ioCommandLogger.isDebugEnabled()) {
127                                             ioCommandLogger.debug("11527 L7vsAdmController::addVirtualService(VirtualSetting vs) out return=false");
128                                                 }
129                                                 // --- debug log (out method) ---
130                                                 return false;
131                                         }
132                                 }
133                         }
134         
135                         // --- debug log (out method) ---
136                         if (ioCommandLogger.isDebugEnabled()) {
137                     ioCommandLogger.debug("11528 L7vsAdmController::addVirtualService(VirtualSetting vs) out return=true");
138                         }
139                         // --- debug log (out method) ---
140                         return true;
141                 }
142         }
143
144         /**
145          * 
146          * <p>
147          * delVirtualService method
148          * </p>
149          * 
150          * @param vs
151          * @return result of l7vsadm delete virtual service command.
152          */
153         public boolean delVirtualService(VirtualSetting vs) {
154         // --- debug log (in method) ---
155         if (ioCommandLogger.isDebugEnabled()) {
156             StringBuffer buf = new StringBuffer();
157             buf.append("L7vsAdmController::delVirtualService(VirtualSetting vs) in ");
158             buf.append("vs=(" + vs + ")");
159             ioCommandLogger.debug("11529 " + buf.toString());
160         }
161         // --- debug log (in method) ---
162
163         synchronized (this) {
164                         if (vs == null || vs.virtual == null) {
165                                 ioCommandLogger.warn("31004 Invalid null argument.");
166                         // --- debug log (out method) ---
167                         if (ioCommandLogger.isDebugEnabled()) {
168                             ioCommandLogger.debug("11530 L7vsAdmController::delVirtualService(VirtualSetting vs) out return=false");
169                         }
170                         // --- debug log (out method) ---
171                                 return false;
172                         }
173                         String command = l7vsadm + " -D -t " + vs.virtual.host + ":"
174                                         + vs.virtual.port + " -m " + vs.protomod + " " + vs.option;
175         
176                         String result = runProcess(command);
177                         if (result == null || result.length() != 0) {
178                                 ioCommandLogger.error("41224 Command error: command=" + command);
179                                 ioCommandLogger.error("41225 Command error:  result=" + result);
180                                 // --- debug log (out method) ---
181                                 if (ioCommandLogger.isDebugEnabled()) {
182                             ioCommandLogger.debug("11531 L7vsAdmController::delVirtualService(VirtualSetting vs) out return=false");
183                                 }
184                                 // --- debug log (out method) ---
185                                 return false;
186                         }
187         
188                         // --- debug log (out method) ---
189                         if (ioCommandLogger.isDebugEnabled()) {
190                     ioCommandLogger.debug("11532 L7vsAdmController::delVirtualService(VirtualSetting vs) out return=true");
191                         }
192                         // --- debug log (out method) ---
193                         return true;
194                 }
195         }
196
197         /**
198          * 
199          * <p>
200          * getData method
201          * </p>
202          * 
203          * @return result of l7vsadm list command, including virtual service
204          *         setting.
205          */
206         public L7vsAdmData getData() {
207         // --- debug log (in method) ---
208         if (ioCommandLogger.isDebugEnabled()) {
209             ioCommandLogger.debug("11533 L7vsAdmController::getData() in");
210         }
211         // --- debug log (in method) ---
212         
213                 synchronized (this) {
214                         L7vsAdmData admData = new L7vsAdmData();
215         
216                         String command = l7vsadm + " -V -n";
217                         String result = runProcess(command);
218         
219                         if (result == null) {
220                                 ioCommandLogger.error("41226 Command error: command=" + command);
221                                 ioCommandLogger.error("41227 Command error:  result=null");
222                                 // --- debug log (out method) ---
223                                 if (ioCommandLogger.isDebugEnabled()) {
224                             ioCommandLogger.debug("11534 L7vsAdmController::getData() out return=null");
225                                 }
226                                 // --- debug log (out method) ---
227                                 return null;
228                         }
229         
230                         // l7vsd log level regex
231                         Pattern l7vsdLogLevel = Pattern.compile(
232                                         "^(l7vsd_[\\w\\.]+)\\s+(debug|info|warn|error|fatal)$",
233                                         Pattern.MULTILINE);
234                         Matcher l7vsdLog = l7vsdLogLevel.matcher(result);
235                         while (l7vsdLog.find()) {
236                                 LogData.LogLevel level;
237                                 if (l7vsdLog.group(2).equals("debug"))
238                                         level = LogData.LogLevel.DEBUG;
239                                 else if (l7vsdLog.group(2).equals("info"))
240                                         level = LogData.LogLevel.INFO;
241                                 else if (l7vsdLog.group(2).equals("warn"))
242                                         level = LogData.LogLevel.WARN;
243                                 else if (l7vsdLog.group(2).equals("error"))
244                                         level = LogData.LogLevel.ERROR;
245                                 else if (l7vsdLog.group(2).equals("fatal"))
246                                         level = LogData.LogLevel.FATAL;
247                                 else {
248                                         ioCommandLogger.info("21092 Unknown log level: " + l7vsdLog.group(2));
249                                         continue;
250                                 }
251         
252                                 if (l7vsdLog.group(1).equals("l7vsd_network"))
253                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_NETWORK,
254                                                         level);
255                                 else if (l7vsdLog.group(1).equals("l7vsd_network.bandwidth"))
256                                         admData.l7vsdLogLevel.put(
257                                                         LogData.LogCategory.L7VSD_NETWORK_BANDWIDTH, level);
258                                 else if (l7vsdLog.group(1).equals("l7vsd_network.num_connection"))
259                                         admData.l7vsdLogLevel
260                                                         .put(LogData.LogCategory.L7VSD_NETWORK_NUM_CONNECTION,
261                                                                         level);
262                                 else if (l7vsdLog.group(1).equals("l7vsd_network.qos"))
263                                         admData.l7vsdLogLevel.put(
264                                                         LogData.LogCategory.L7VSD_NETWORK_QOS, level);
265                                 else if (l7vsdLog.group(1).equals("l7vsd_virtual_service"))
266                                         admData.l7vsdLogLevel.put(
267                                                         LogData.LogCategory.L7VSD_VIRTUAL_SERVICE, level);
268                                 else if (l7vsdLog.group(1).equals("l7vsd_real_server"))
269                                         admData.l7vsdLogLevel.put(
270                                                         LogData.LogCategory.L7VSD_REAL_SERVER, level);
271                                 else if (l7vsdLog.group(1).equals("l7vsd_sorry_server"))
272                                         admData.l7vsdLogLevel.put(
273                                                         LogData.LogCategory.L7VSD_SORRY_SERVER, level);
274                                 else if (l7vsdLog.group(1).equals("l7vsd_real_server.balancing"))
275                                         admData.l7vsdLogLevel.put(
276                                                         LogData.LogCategory.L7VSD_REAL_SERVER_BALANCING, level);
277                                 else if (l7vsdLog.group(1).equals("l7vsd_replication"))
278                                         admData.l7vsdLogLevel.put(
279                                                         LogData.LogCategory.L7VSD_REPLICATION, level);
280                                 else if (l7vsdLog.group(1).equals("l7vsd_start_stop"))
281                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_START_STOP,
282                                                         level);
283                                 else if (l7vsdLog.group(1).equals("l7vsd_system"))
284                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_SYSTEM,
285                                                         level);
286                                 else if (l7vsdLog.group(1).equals("l7vsd_system.memory"))
287                                         admData.l7vsdLogLevel.put(
288                                                         LogData.LogCategory.L7VSD_SYSTEM_MEMORY, level);
289                                 else if (l7vsdLog.group(1).equals("l7vsd_system.socket"))
290                                         admData.l7vsdLogLevel.put(
291                                                         LogData.LogCategory.L7VSD_SYSTEM_SOCKET, level);
292                                 else if (l7vsdLog.group(1).equals("l7vsd_system.signal"))
293                                         admData.l7vsdLogLevel.put(
294                                                         LogData.LogCategory.L7VSD_SYSTEM_SIGNAL, level);
295                                 else if (l7vsdLog.group(1).equals("l7vsd_environment"))
296                                         admData.l7vsdLogLevel.put(
297                                                         LogData.LogCategory.L7VSD_ENVIRONMENT, level);
298                                 else if (l7vsdLog.group(1).equals("l7vsd_environment.parameter"))
299                                         admData.l7vsdLogLevel.put(
300                                                         LogData.LogCategory.L7VSD_ENVIRONMENT_PARAMETER, level);
301                                 else if (l7vsdLog.group(1).equals("l7vsd_logger"))
302                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_LOGGER,
303                                                         level);
304                                 else if (l7vsdLog.group(1).equals("l7vsd_parameter"))
305                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_PARAMETER,
306                                                         level);
307                                 else if (l7vsdLog.group(1).equals("l7vsd_event"))
308                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_EVENT,
309                                                         level);
310                                 else if (l7vsdLog.group(1).equals("l7vsd_schedule"))
311                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_SCHEDULE,
312                                                         level);
313                                 else if (l7vsdLog.group(1).equals("l7vsd_program"))
314                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_PROGRAM,
315                                                         level);
316                                 else if (l7vsdLog.group(1).equals("l7vsd_protocol"))
317                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_PROTOCOL,
318                                                         level);
319                                 else if (l7vsdLog.group(1).equals("l7vsd_module"))
320                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_MODULE,
321                                                         level);
322                                 else if (l7vsdLog.group(1).equals("l7vsd_all"))
323                                         admData.l7vsdLogLevel.put(LogData.LogCategory.L7VSD_ALL, level);
324                                 else {
325                                         ioCommandLogger.info("21093 Unknown log category: " + l7vsdLog.group(1));
326                                         continue;
327                                 }
328                         }
329         
330                         // SNMP Agent log level regex
331                         Pattern snmpLogLevel = Pattern.compile(
332                                         "^(snmpagent_\\w+)\\s+(debug|info|warn|error|fatal)$",
333                                         Pattern.MULTILINE);
334                         Matcher snmpLog = snmpLogLevel.matcher(result);
335                         while (snmpLog.find()) {
336                                 LogData.LogLevel level;
337                                 if (snmpLog.group(2).equals("debug"))
338                                         level = LogData.LogLevel.DEBUG;
339                                 else if (snmpLog.group(2).equals("info"))
340                                         level = LogData.LogLevel.INFO;
341                                 else if (snmpLog.group(2).equals("warn"))
342                                         level = LogData.LogLevel.WARN;
343                                 else if (snmpLog.group(2).equals("error"))
344                                         level = LogData.LogLevel.ERROR;
345                                 else if (snmpLog.group(2).equals("fatal"))
346                                         level = LogData.LogLevel.FATAL;
347                                 else {
348                                         ioCommandLogger.info("21094 Unknown log level: " + snmpLog.group(2));
349                                         continue;
350                                 }
351         
352                                 if (snmpLog.group(1).equals("snmpagent_start_stop"))
353                                         admData.snmpLogLevel.put(
354                                                         LogData.LogCategory.SNMPAGENT_START_STOP, level);
355                                 else if (snmpLog.group(1).equals("snmpagent_manager_receive"))
356                                         admData.snmpLogLevel.put(
357                                                         LogData.LogCategory.SNMPAGENT_MANAGER_RECEIVE, level);
358                                 else if (snmpLog.group(1).equals("snmpagent_manager_send"))
359                                         admData.snmpLogLevel.put(
360                                                         LogData.LogCategory.SNMPAGENT_MANAGER_SEND, level);
361                                 else if (snmpLog.group(1).equals("snmpagent_l7vsd_receive"))
362                                         admData.snmpLogLevel.put(
363                                                         LogData.LogCategory.SNMPAGENT_L7VSD_RECEIVE, level);
364                                 else if (snmpLog.group(1).equals("snmpagent_l7vsd_send"))
365                                         admData.snmpLogLevel.put(
366                                                         LogData.LogCategory.SNMPAGENT_L7VSD_SEND, level);
367                                 else if (snmpLog.group(1).equals("snmpagent_logger"))
368                                         admData.snmpLogLevel.put(LogData.LogCategory.SNMPAGENT_LOGGER,
369                                                         level);
370                                 else if (snmpLog.group(1).equals("snmpagent_parameter"))
371                                         admData.snmpLogLevel.put(
372                                                         LogData.LogCategory.SNMPAGENT_PARAMETER, level);
373                                 else if (snmpLog.group(1).equals("snmpagent_all"))
374                                         admData.snmpLogLevel.put(LogData.LogCategory.SNMPAGENT_ALL,
375                                                         level);
376                                 else {
377                                         ioCommandLogger.info("21095 Unknown log level: " + snmpLog.group(1));
378                                         continue;
379                                 }
380                         }
381         
382                         // Replication mode regex
383                         Pattern replicationMode = Pattern.compile(
384                                         "^Replication Mode:\\s+(\\w+)", Pattern.MULTILINE);
385                         Matcher rep = replicationMode.matcher(result);
386                         if (rep.find()) {
387                                 if (rep.group(1).equals("MASTER"))
388                                         admData.replicationMode = ReplicationStatus.MASTER;
389                                 else if (rep.group(1).equals("MASTER_STOP"))
390                                         admData.replicationMode = ReplicationStatus.MASTER_STOP;
391                                 else if (rep.group(1).equals("SLAVE"))
392                                         admData.replicationMode = ReplicationStatus.SLAVE;
393                                 else if (rep.group(1).equals("SLAVE_STOP"))
394                                         admData.replicationMode = ReplicationStatus.SLAVE_STOP;
395                                 else if (rep.group(1).equals("SINGLE"))
396                                         admData.replicationMode = ReplicationStatus.SINGLE;
397                                 else if (rep.group(1).equals("OUT"))
398                                         admData.replicationMode = ReplicationStatus.OUT;
399                                 else {
400                                         ioCommandLogger.info("21096 Unknown replication status: " + rep.group(1));
401                                 }
402                         }
403         
404                         // SNMP Agent connection status regex
405                         Pattern snmpStatus = Pattern.compile(
406                                         "^SNMPAgent Connection Status:\\s+([-\\w]+)", Pattern.MULTILINE);
407                         Matcher snmp = snmpStatus.matcher(result);
408                         if (snmp.find()) {
409                                 if (snmp.group(1).equals("connecting"))
410                                         admData.snmpStatus = SnmpAgentStatus.CONNECTING;
411                                 else if (snmp.group(1).equals("non-connecting"))
412                                         admData.snmpStatus = SnmpAgentStatus.NON_CONNECTING;
413                                 else {
414                                         ioCommandLogger.info("21097 Unknown SNMP Agent status: " + snmp.group(1));
415                                 }
416                         }
417         
418                         VirtualSetting vs = null;
419         
420                         // Virtual service regex
421                         String virtualLine = "TCP (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5}) (\\w+) "
422                                         + "(\\w+) (0|1) (.*)\\s+([^\\s]+) (\\d+) (0|1)\\s+(\\d+) (\\d+) (\\d+)\\s*";
423                         // Real server regex
424                         String realLine = "\\s+-> (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5})\\s+Masq\\s+"
425                                         + "(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*";
426         
427                         Pattern virtualService = Pattern.compile("^(?:" + virtualLine + "|"
428                                         + realLine + ")$", Pattern.MULTILINE);
429                         Matcher virtual = virtualService.matcher(result);
430                         while (virtual.find()) {
431         
432                                 if (virtual.group(1) != null) { // virtual service line found
433                                         if (vs != null)
434                                                 admData.virtualSettings.add(vs);
435                                         vs = new VirtualSetting();
436         
437                                         vs.virtual = new EndPoint(virtual.group(1), virtual.group(2));
438                                         vs.protomod = virtual.group(3);
439                                         vs.sched = virtual.group(4);
440                                         vs.reschedule = virtual.group(5).equals("0") ? false : true;
441                                         vs.option = virtual.group(6);
442                                         String[] sorry = virtual.group(7).split(":");
443                                         if (sorry.length == 2 && sorry[1] != null) {
444                                                 vs.sorryserver = new EndPoint(sorry[0], sorry[1]);
445                                         }
446                                         vs.maxconn = Integer.parseInt(virtual.group(8));
447                                         vs.sorryflag = virtual.group(9).equals("0") ? false : true;
448                                         vs.qosservice = Long.parseLong(virtual.group(10));
449                                         vs.qosclient = Long.parseLong(virtual.group(11));
450                                         vs.throughput = Long.parseLong(virtual.group(12));
451                                 } else { // real server line found
452                                         if (vs.real == null)
453                                                 vs.real = new Vector<EndPoint>();
454                                         vs.real.add(new EndPoint(virtual.group(13), virtual.group(14), Integer.parseInt(virtual
455                                                         .group(15)), Long.parseLong(virtual.group(16)), Long
456                                                         .parseLong(virtual.group(17))));
457                                 }
458                         }
459                         if (vs != null)
460                                 admData.virtualSettings.add(vs);
461         
462                         // --- debug log (out method) ---
463                         if (ioCommandLogger.isDebugEnabled()) {
464                     ioCommandLogger.debug("11535 L7vsAdmController::getData() out return=" + admData);
465                         }
466                         // --- debug log (out method) ---
467                         return admData;
468                 }
469         }
470
471         /**
472          * 
473          * <p>
474          * setReplicationMode method
475          * </p>
476          * 
477          * @param mode
478          * @return result of l7vsadm change replication mode command.
479          */
480         public boolean setReplicationMode(ReplicationStatus mode) {
481         // --- debug log (in method) ---
482         if (ioCommandLogger.isDebugEnabled()) {
483             StringBuffer buf = new StringBuffer();
484             buf.append("L7vsAdmController::setReplicationMode(ReplicationStatus mode) in ");
485             buf.append("mode=" + mode);
486             ioCommandLogger.debug("11536 " + buf.toString());
487         }
488         // --- debug log (in method) ---
489
490                 synchronized (this) {
491                         if (mode == null) {
492                                 ioCommandLogger.error("41228 Invalid replication status: " + mode);
493                         // --- debug log (out method) ---
494                         if (ioCommandLogger.isDebugEnabled()) {
495                             ioCommandLogger.debug("11537 L7vsAdmController::setReplicationMode(ReplicationStatus mode) out return=false");
496                         }
497                         // --- debug log (out method) ---
498                                 return false;
499                         }
500                         
501                         String command = l7vsadm + " -R -s ";
502                         switch (mode) {
503                         case MASTER:
504                         case SLAVE:
505                                 command += "start";
506                                 break;
507                         case MASTER_STOP:
508                         case SLAVE_STOP:
509                                 command += "stop";
510                                 break;
511                         default:
512                                 ioCommandLogger.error("41229 Invalid replication status: " + mode);
513                         // --- debug log (out method) ---
514                         if (ioCommandLogger.isDebugEnabled()) {
515                             ioCommandLogger.debug("11538 L7vsAdmController::setReplicationMode(ReplicationStatus mode) out return=false");
516                         }
517                         // --- debug log (out method) ---
518                                 return false;
519                         }
520         
521                         String result = runProcess(command);
522                         if (result == null || result.length() != 0) {
523                                 ioCommandLogger.error("41230 Command error: command=" + command);
524                                 ioCommandLogger.error("41231 Command error:  result=" + result);
525                         // --- debug log (out method) ---
526                         if (ioCommandLogger.isDebugEnabled()) {
527                             ioCommandLogger.debug("11539 L7vsAdmController::setReplicationMode(ReplicationStatus mode) out return=false");
528                         }
529                         // --- debug log (out method) ---
530                                 return false;
531                         }
532         
533                 // --- debug log (out method) ---
534                 if (ioCommandLogger.isDebugEnabled()) {
535                     ioCommandLogger.debug("11540 L7vsAdmController::setReplicationMode(ReplicationStatus mode) out return=true");
536                 }
537                 // --- debug log (out method) ---
538                         return true;
539                 }
540         }
541
542         /**
543          * 
544          * <p>
545          * setLogLevel method
546          * </p>
547          * 
548          * @param category
549          * @return result of l7vsadm set log level command.
550          */
551         public boolean setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) {
552         // --- debug log (in method) ---
553         if (ioCommandLogger.isDebugEnabled()) {
554             StringBuffer buf = new StringBuffer();
555             buf.append("L7vsAdmController::setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) in ");
556             buf.append("category=" + category);
557             ioCommandLogger.debug("11541 " + buf.toString());
558         }
559         // --- debug log (in method) ---
560                 
561                 synchronized (this) {
562                         if (category == null) {
563                                 ioCommandLogger.error("41232 Invalid log category map: " + category);
564                         // --- debug log (out method) ---
565                         if (ioCommandLogger.isDebugEnabled()) {
566                             ioCommandLogger.debug("11542 L7vsAdmController::setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) out return=false");
567                         }
568                         // --- debug log (out method) ---
569                                 return false;
570                         }
571                         
572                         Iterator<LogData.LogCategory> keys = category.keySet().iterator();
573                         while (keys.hasNext()) {
574                                 String command = l7vsadm;
575                                 LogData.LogCategory c = (LogData.LogCategory) keys.next();
576                                 switch (c) {
577                                 case L7VSD_NETWORK:
578                                         switch (category.get(c)) {
579                                         case DEBUG:
580                                         case INFO:
581                                         case WARN:
582                                         case ERROR:
583                                         case FATAL:
584                                                 command += " -L -c l7vsd_network";
585                                                 break;
586                                         default:
587                                                 continue;
588                                         }
589                                         break;
590                                 case L7VSD_NETWORK_BANDWIDTH:
591                                         switch (category.get(c)) {
592                                         case DEBUG:
593                                         case INFO:
594                                         case WARN:
595                                         case ERROR:
596                                         case FATAL:
597                                                 command += " -L -c l7vsd_network.bandwidth";
598                                                 break;
599                                         default:
600                                                 continue;
601                                         }
602                                         break;
603                                 case L7VSD_NETWORK_NUM_CONNECTION:
604                                         switch (category.get(c)) {
605                                         case DEBUG:
606                                         case INFO:
607                                         case WARN:
608                                         case ERROR:
609                                         case FATAL:
610                                                 command += " -L -c l7vsd_network.num_connection";
611                                                 break;
612                                         default:
613                                                 continue;
614                                         }
615                                         break;
616                                 case L7VSD_NETWORK_QOS:
617                                         switch (category.get(c)) {
618                                         case DEBUG:
619                                         case INFO:
620                                         case WARN:
621                                         case ERROR:
622                                         case FATAL:
623                                                 command += " -L -c l7vsd_network.qos";
624                                                 break;
625                                         default:
626                                                 continue;
627                                         }
628                                         break;
629                                 case L7VSD_VIRTUAL_SERVICE:
630                                         switch (category.get(c)) {
631                                         case DEBUG:
632                                         case INFO:
633                                         case WARN:
634                                         case ERROR:
635                                         case FATAL:
636                                                 command += " -L -c l7vsd_virtual_service";
637                                                 break;
638                                         default:
639                                                 continue;
640                                         }
641                                         break;
642                                 case L7VSD_REAL_SERVER:
643                                         switch (category.get(c)) {
644                                         case DEBUG:
645                                         case INFO:
646                                         case WARN:
647                                         case ERROR:
648                                         case FATAL:
649                                                 command += " -L -c l7vsd_real_server";
650                                                 break;
651                                         default:
652                                                 continue;
653                                         }
654                                         break;
655                                 case L7VSD_SORRY_SERVER:
656                                         switch (category.get(c)) {
657                                         case DEBUG:
658                                         case INFO:
659                                         case WARN:
660                                         case ERROR:
661                                         case FATAL:
662                                                 command += " -L -c l7vsd_sorry_server";
663                                                 break;
664                                         default:
665                                                 continue;
666                                         }
667                                         break;
668                                 case L7VSD_REAL_SERVER_BALANCING:
669                                         switch (category.get(c)) {
670                                         case DEBUG:
671                                         case INFO:
672                                         case WARN:
673                                         case ERROR:
674                                         case FATAL:
675                                                 command += " -L -c l7vsd_real_server.balancing";
676                                                 break;
677                                         default:
678                                                 continue;
679                                         }
680                                         break;
681                                 case L7VSD_REPLICATION:
682                                         switch (category.get(c)) {
683                                         case DEBUG:
684                                         case INFO:
685                                         case WARN:
686                                         case ERROR:
687                                         case FATAL:
688                                                 command += " -L -c l7vsd_replication";
689                                                 break;
690                                         default:
691                                                 continue;
692                                         }
693                                         break;
694                                 case L7VSD_START_STOP:
695                                         switch (category.get(c)) {
696                                         case DEBUG:
697                                         case INFO:
698                                         case WARN:
699                                         case ERROR:
700                                         case FATAL:
701                                                 command += " -L -c l7vsd_start_stop";
702                                                 break;
703                                         default:
704                                                 continue;
705                                         }
706                                         break;
707                                 case L7VSD_SYSTEM:
708                                         switch (category.get(c)) {
709                                         case DEBUG:
710                                         case INFO:
711                                         case WARN:
712                                         case ERROR:
713                                         case FATAL:
714                                                 command += " -L -c l7vsd_system";
715                                                 break;
716                                         default:
717                                                 continue;
718                                         }
719                                         break;
720                                 case L7VSD_SYSTEM_MEMORY:
721                                         switch (category.get(c)) {
722                                         case DEBUG:
723                                         case INFO:
724                                         case WARN:
725                                         case ERROR:
726                                         case FATAL:
727                                                 command += " -L -c l7vsd_system.memory";
728                                                 break;
729                                         default:
730                                                 continue;
731                                         }
732                                         break;
733                                 case L7VSD_SYSTEM_SOCKET:
734                                         switch (category.get(c)) {
735                                         case DEBUG:
736                                         case INFO:
737                                         case WARN:
738                                         case ERROR:
739                                         case FATAL:
740                                                 command += " -L -c l7vsd_system.socket";
741                                                 break;
742                                         default:
743                                                 continue;
744                                         }
745                                         break;
746                                 case L7VSD_SYSTEM_SIGNAL:
747                                         switch (category.get(c)) {
748                                         case DEBUG:
749                                         case INFO:
750                                         case WARN:
751                                         case ERROR:
752                                         case FATAL:
753                                                 command += " -L -c l7vsd_system.signal";
754                                                 break;
755                                         default:
756                                                 continue;
757                                         }
758                                         break;
759                                 case L7VSD_ENVIRONMENT:
760                                         switch (category.get(c)) {
761                                         case DEBUG:
762                                         case INFO:
763                                         case WARN:
764                                         case ERROR:
765                                         case FATAL:
766                                                 command += " -L -c l7vsd_environment";
767                                                 break;
768                                         default:
769                                                 continue;
770                                         }
771                                         break;
772                                 case L7VSD_ENVIRONMENT_PARAMETER:
773                                         switch (category.get(c)) {
774                                         case DEBUG:
775                                         case INFO:
776                                         case WARN:
777                                         case ERROR:
778                                         case FATAL:
779                                                 command += " -L -c l7vsd_environment.parameter";
780                                                 break;
781                                         default:
782                                                 continue;
783                                         }
784                                         break;
785                                 case L7VSD_LOGGER:
786                                         switch (category.get(c)) {
787                                         case DEBUG:
788                                         case INFO:
789                                         case WARN:
790                                         case ERROR:
791                                         case FATAL:
792                                                 command += " -L -c l7vsd_logger";
793                                                 break;
794                                         default:
795                                                 continue;
796                                         }
797                                         break;
798                                 case L7VSD_PARAMETER:
799                                         switch (category.get(c)) {
800                                         case DEBUG:
801                                         case INFO:
802                                         case WARN:
803                                         case ERROR:
804                                         case FATAL:
805                                                 command += " -L -c l7vsd_parameter";
806                                                 break;
807                                         default:
808                                                 continue;
809                                         }
810                                         break;
811                                 case L7VSD_EVENT:
812                                         switch (category.get(c)) {
813                                         case DEBUG:
814                                         case INFO:
815                                         case WARN:
816                                         case ERROR:
817                                         case FATAL:
818                                                 command += " -L -c l7vsd_event";
819                                                 break;
820                                         default:
821                                                 continue;
822                                         }
823                                         break;
824                                 case L7VSD_SCHEDULE:
825                                         switch (category.get(c)) {
826                                         case DEBUG:
827                                         case INFO:
828                                         case WARN:
829                                         case ERROR:
830                                         case FATAL:
831                                                 command += " -L -c l7vsd_schedule";
832                                                 break;
833                                         default:
834                                                 continue;
835                                         }
836                                         break;
837                                 case L7VSD_PROGRAM:
838                                         switch (category.get(c)) {
839                                         case DEBUG:
840                                         case INFO:
841                                         case WARN:
842                                         case ERROR:
843                                         case FATAL:
844                                                 command += " -L -c l7vsd_program";
845                                                 break;
846                                         default:
847                                                 continue;
848                                         }
849                                         break;
850                                 case L7VSD_PROTOCOL:
851                                         switch (category.get(c)) {
852                                         case DEBUG:
853                                         case INFO:
854                                         case WARN:
855                                         case ERROR:
856                                         case FATAL:
857                                                 command += " -L -c l7vsd_protocol";
858                                                 break;
859                                         default:
860                                                 continue;
861                                         }
862                                         break;
863                                 case L7VSD_MODULE:
864                                         switch (category.get(c)) {
865                                         case DEBUG:
866                                         case INFO:
867                                         case WARN:
868                                         case ERROR:
869                                         case FATAL:
870                                                 command += " -L -c l7vsd_module";
871                                                 break;
872                                         default:
873                                                 continue;
874                                         }
875                                         break;
876                                 case L7VSD_ALL:
877                                         switch (category.get(c)) {
878                                         case DEBUG:
879                                         case INFO:
880                                         case WARN:
881                                         case ERROR:
882                                         case FATAL:
883                                                 command += " -L -c all";
884                                                 break;
885                                         default:
886                                                 continue;
887                                         }
888                                         break;
889                                 case SNMPAGENT_START_STOP:
890                                         switch (category.get(c)) {
891                                         case DEBUG:
892                                         case INFO:
893                                         case WARN:
894                                         case ERROR:
895                                         case FATAL:
896                                                 command += " -S -c snmpagent_start_stop";
897                                                 break;
898                                         default:
899                                                 continue;
900                                         }
901                                         break;
902                                 case SNMPAGENT_MANAGER_RECEIVE:
903                                         switch (category.get(c)) {
904                                         case DEBUG:
905                                         case INFO:
906                                         case WARN:
907                                         case ERROR:
908                                         case FATAL:
909                                                 command += " -S -c snmpagent_manager_receive";
910                                                 break;
911                                         default:
912                                                 continue;
913                                         }
914                                         break;
915                                 case SNMPAGENT_MANAGER_SEND:
916                                         switch (category.get(c)) {
917                                         case DEBUG:
918                                         case INFO:
919                                         case WARN:
920                                         case ERROR:
921                                         case FATAL:
922                                                 command += " -S -c snmpagent_manager_send";
923                                                 break;
924                                         default:
925                                                 continue;
926                                         }
927                                         break;
928                                 case SNMPAGENT_L7VSD_RECEIVE:
929                                         switch (category.get(c)) {
930                                         case DEBUG:
931                                         case INFO:
932                                         case WARN:
933                                         case ERROR:
934                                         case FATAL:
935                                                 command += " -S -c snmpagent_l7vsd_receive";
936                                                 break;
937                                         default:
938                                                 continue;
939                                         }
940                                         break;
941                                 case SNMPAGENT_L7VSD_SEND:
942                                         switch (category.get(c)) {
943                                         case DEBUG:
944                                         case INFO:
945                                         case WARN:
946                                         case ERROR:
947                                         case FATAL:
948                                                 command += " -S -c snmpagent_l7vsd_send";
949                                                 break;
950                                         default:
951                                                 continue;
952                                         }
953                                         break;
954                                 case SNMPAGENT_LOGGER:
955                                         switch (category.get(c)) {
956                                         case DEBUG:
957                                         case INFO:
958                                         case WARN:
959                                         case ERROR:
960                                         case FATAL:
961                                                 command += " -S -c snmpagent_logger";
962                                                 break;
963                                         default:
964                                                 continue;
965                                         }
966                                         break;
967                                 case SNMPAGENT_PARAMETER:
968                                         switch (category.get(c)) {
969                                         case DEBUG:
970                                         case INFO:
971                                         case WARN:
972                                         case ERROR:
973                                         case FATAL:
974                                                 command += " -S -c snmpagent_parameter";
975                                                 break;
976                                         default:
977                                                 continue;
978                                         }
979                                         break;
980                                 case SNMPAGENT_ALL:
981                                         switch (category.get(c)) {
982                                         case DEBUG:
983                                         case INFO:
984                                         case WARN:
985                                         case ERROR:
986                                         case FATAL:
987                                                 command += " -S -c all";
988                                                 break;
989                                         default:
990                                                 continue;
991                                         }
992                                         break;
993                                 default:
994                                         ioCommandLogger.info("21098 Unknown log category: " + c);
995                                 // --- debug log (out method) ---
996                                 if (ioCommandLogger.isDebugEnabled()) {
997                                     ioCommandLogger.debug("11543 L7vsAdmController::setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) out return=false");
998                                 }
999                                 // --- debug log (out method) ---
1000                                         return false;
1001                                 }
1002         
1003                                 switch (category.get(c)) {
1004                                 case DEBUG:
1005                                         command += " -l debug";
1006                                         break;
1007                                 case INFO:
1008                                         command += " -l info";
1009                                         break;
1010                                 case WARN:
1011                                         command += " -l warn";
1012                                         break;
1013                                 case ERROR:
1014                                         command += " -l error";
1015                                         break;
1016                                 case FATAL:
1017                                         command += " -l fatal";
1018                                         break;
1019                                 default:
1020                                         ioCommandLogger.info("21099 Unknown log level: " + category.get(c));
1021                                 // --- debug log (out method) ---
1022                                 if (ioCommandLogger.isDebugEnabled()) {
1023                                     ioCommandLogger.debug("11544 L7vsAdmController::setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) out return=false");
1024                                 }
1025                                 // --- debug log (out method) ---
1026                                         return false;
1027                                 }
1028                                 
1029                                 String result = runProcess(command);
1030                                 if (result == null || result.length() != 0) {
1031                                         ioCommandLogger.error("41233 Command error: command=" + command);
1032                                         ioCommandLogger.error("41234 Command error:  result=" + result);
1033                                 // --- debug log (out method) ---
1034                                 if (ioCommandLogger.isDebugEnabled()) {
1035                                     ioCommandLogger.debug("11545 L7vsAdmController::setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) out return=false");
1036                                 }
1037                                 // --- debug log (out method) ---
1038                                         return false;
1039                                 }
1040                         }
1041                 // --- debug log (out method) ---
1042                 if (ioCommandLogger.isDebugEnabled()) {
1043                     ioCommandLogger.debug("11546 L7vsAdmController::setLogLevel(HashMap<LogData.LogCategory, LogData.LogLevel> category) out return=true");
1044                 }
1045                 // --- debug log (out method) ---
1046                         return true;
1047                 }
1048         }
1049
1050         /**
1051          * 
1052          * <p>
1053          * dumpReplicationData method
1054          * </p>
1055          * 
1056          * @return result of l7vsadm replication dump command.
1057          */
1058         public boolean dumpReplicationData() {
1059         // --- debug log (in method) ---
1060         if (ioCommandLogger.isDebugEnabled()) {
1061             ioCommandLogger.debug("11547 L7vsAdmController::dumpReplicationData() in");
1062         }
1063         // --- debug log (in method) ---
1064
1065         synchronized (this) {
1066                         String command = l7vsadm + " -R -d";
1067                         String result = runProcess(command);
1068                         if (result == null || result.length() != 0) {
1069                                 ioCommandLogger.error("41235 Command error: command=" + command);
1070                                 ioCommandLogger.error("41236 Command error:  result=" + result);
1071                         // --- debug log (out method) ---
1072                         if (ioCommandLogger.isDebugEnabled()) {
1073                             ioCommandLogger.debug("11548 L7vsAdmController::dumpReplicationData() out return=false");
1074                         }
1075                         // --- debug log (out method) ---
1076                                 return false;
1077                         }
1078         
1079                 // --- debug log (out method) ---
1080                 if (ioCommandLogger.isDebugEnabled()) {
1081                     ioCommandLogger.debug("11549 L7vsAdmController::dumpReplicationData() out return=true");
1082                 }
1083                 // --- debug log (out method) ---
1084                         return true;
1085                 }
1086         }
1087
1088         /**
1089          * 
1090          * <p>
1091          * reloadConf method
1092          * </p>
1093          * 
1094          * @param c
1095          * @return result of l7vsadm reload config command.
1096          */
1097         public boolean reloadConf(L7vsConfCategory c) {
1098         // --- debug log (in method) ---
1099         if (ioCommandLogger.isDebugEnabled()) {
1100             StringBuffer buf = new StringBuffer();
1101             buf.append("L7vsAdmController::reloadConf(L7vsConfCategory c) in ");
1102             buf.append("c=" + c);
1103             ioCommandLogger.debug("11550 " + buf.toString());
1104         }
1105         // --- debug log (in method) ---
1106                 
1107                 synchronized (this) {
1108                         if (c == null) {
1109                                 ioCommandLogger.error("41237 Invalid argument: " + c);
1110                         // --- debug log (out method) ---
1111                         if (ioCommandLogger.isDebugEnabled()) {
1112                             ioCommandLogger.debug("11551 L7vsAdmController::reloadConf(L7vsConfCategory c) out return=false");
1113                         }
1114                         // --- debug log (out method) ---
1115                                 return false;
1116                         }
1117                         
1118                         String command = l7vsadm + " -P -r ";
1119                         switch (c) {
1120                         case ALL:
1121                                 command += "all";
1122                                 break;
1123                         case L7VSD:
1124                                 command += "l7vsd";
1125                                 break;
1126                         case IOMUX:
1127                                 command += "iomux";
1128                                 break;
1129                         case LSOCK:
1130                                 command += "lsock";
1131                                 break;
1132                         case CONN:
1133                                 command += "conn";
1134                                 break;
1135                         case DEST:
1136                                 command += "dest";
1137                                 break;
1138                         case SERVICE:
1139                                 command += "service";
1140                                 break;
1141                         case MODULE:
1142                                 command += "module";
1143                                 break;
1144                         case REPLICATION:
1145                                 command += "replication";
1146                                 break;
1147                         case LOGGER:
1148                                 command += "logger";
1149                                 break;
1150                         case L7VSADM:
1151                                 command += "l7vsadm";
1152                                 break;
1153                         case SNMPAGENT:
1154                                 command += "snmpagent";
1155                                 break;
1156                         default:
1157                                 ioCommandLogger.error("41238 Unknown log category: " + c);
1158                         // --- debug log (out method) ---
1159                         if (ioCommandLogger.isDebugEnabled()) {
1160                             ioCommandLogger.debug("11552 L7vsAdmController::reloadConf(L7vsConfCategory c) out return=false");
1161                         }
1162                         // --- debug log (out method) ---
1163                                 return false;
1164                         }
1165         
1166                         String result = runProcess(command);
1167                         if (result == null || result.length() != 0) {
1168                                 ioCommandLogger.error("41239 Command error: command=" + command);
1169                                 ioCommandLogger.error("41240 Command error:  result=" + result);
1170                         // --- debug log (out method) ---
1171                         if (ioCommandLogger.isDebugEnabled()) {
1172                             ioCommandLogger.debug("11553 L7vsAdmController::reloadConf(L7vsConfCategory c) out return=false");
1173                         }
1174                         // --- debug log (out method) ---
1175                                 return false;
1176                         }
1177         
1178                 // --- debug log (out method) ---
1179                 if (ioCommandLogger.isDebugEnabled()) {
1180                     ioCommandLogger.debug("11554 L7vsAdmController::reloadConf(L7vsConfCategory c) out return=true");
1181                 }
1182                 // --- debug log (out method) ---
1183                         return true;
1184                 }
1185         }
1186
1187         /**
1188          * 
1189          * <p>
1190          * runProcess method
1191          * </p>
1192          * 
1193          * @param command
1194          * @return result string of command execution
1195          */
1196         protected String runProcess(String command) {
1197                 // --- debug log (in method) ---
1198                 if (ioCommandLogger.isDebugEnabled()) {
1199             StringBuffer buf = new StringBuffer();
1200                         buf.append("L7vsAdmController::runProcess(String command) in ");
1201                         buf.append("command=\"" + command + "\"");
1202                         ioCommandLogger.debug("11555 " + buf.toString());
1203                 }
1204                 // --- debug log (in method) ---
1205
1206                 synchronized (this) {
1207                         StringBuffer result = new StringBuffer();
1208         
1209                         try {
1210                                 // TODO using sudo command temporally
1211                                 Process ps = Runtime.getRuntime().exec("sudo " + command);
1212                                 InputStream stderr = ps.getErrorStream();
1213                                 BufferedReader br = new BufferedReader(new InputStreamReader(stderr));
1214                                 String line = null;
1215                                 while ((line = br.readLine()) != null) {
1216                                         result.append(line + "\n");
1217                                 }
1218                                 InputStream stdout = ps.getInputStream();
1219                                 br = new BufferedReader(new InputStreamReader(stdout));
1220                                 while ((line = br.readLine()) != null) {
1221                                         result.append(line + "\n");
1222                                 }
1223         
1224                                 Pattern p = Pattern
1225                                                 .compile("^COMMON ERROR.*timeout");
1226                                 Matcher m = p.matcher(result);
1227                                 if (m.find()) {
1228                                         Parameter param = Parameter.getInstance();
1229                                         param.setValue(Parameter.L7VSADM_TIMEOUT_FLAG, "yes");
1230                                 } else {
1231                                         Parameter param = Parameter.getInstance();
1232                                         param.setValue(Parameter.L7VSADM_TIMEOUT_FLAG, null);
1233                                 }
1234                         } catch (Exception e) {
1235                                 ioCommandLogger.error("41241 Exception occured: " + e.getMessage());
1236                                 result = null;
1237                         }
1238         
1239                         // --- debug log (out method) ---
1240                         if (ioCommandLogger.isDebugEnabled()) {
1241                     ioCommandLogger.debug("11556 L7vsAdmController::runProcess(String command) out return=" + result);
1242                         }
1243                         // --- debug log (out method) ---
1244                         return (result == null) ? null : result.toString();
1245                 }
1246         }
1247 }