2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/08/19 13:42:30 $
35 #include "stg/logger.h"
36 #include "stg/dotconfpp.h"
37 #include "settings_impl.h"
39 //-----------------------------------------------------------------------------
40 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
41 : modulesPath("/usr/lib/stg"),
43 confDir(cd.empty() ? "/etc/stargazer" : cd),
45 rules(confDir + "/rules"),
46 logFile("/var/log/stargazer.log"),
47 pidFile("/var/run/stargazer.pid"),
48 monitorDir("/var/stargazer/monitoring"),
50 detailStatWritePeriod(dsPeriod_1_6),
58 freeMbAllowInet(false),
59 dayFeeIsLastDay(false),
61 writeFreeMbTraffCost(false),
65 reconnectOnTariffChange(false),
66 disableSessionLog(false),
67 logger(GetStgLogger())
69 filterParamsLog.push_back("*");
71 //-----------------------------------------------------------------------------
72 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
73 : modulesPath(rval.modulesPath),
74 dirName(rval.dirName),
75 confDir(rval.confDir),
76 scriptsDir(rval.scriptsDir),
78 logFile(rval.logFile),
79 pidFile(rval.pidFile),
80 monitorDir(rval.monitorDir),
81 monitoring(rval.monitoring),
82 detailStatWritePeriod(rval.detailStatWritePeriod),
83 statWritePeriod(rval.statWritePeriod),
84 stgExecMsgKey(rval.stgExecMsgKey),
85 executersNum(rval.executersNum),
86 fullFee(rval.fullFee),
88 dayResetTraff(rval.dayResetTraff),
89 spreadFee(rval.spreadFee),
90 freeMbAllowInet(rval.freeMbAllowInet),
91 dayFeeIsLastDay(rval.dayFeeIsLastDay),
92 stopOnError(rval.stopOnError),
93 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
94 showFeeInCash(rval.showFeeInCash),
95 messageTimeout(rval.messageTimeout),
96 feeChargeType(rval.feeChargeType),
97 reconnectOnTariffChange(rval.reconnectOnTariffChange),
98 disableSessionLog(rval.disableSessionLog),
99 filterParamsLog(rval.filterParamsLog),
100 modulesSettings(rval.modulesSettings),
101 storeModuleSettings(rval.storeModuleSettings),
102 logger(GetStgLogger())
105 //-----------------------------------------------------------------------------
106 SETTINGS_IMPL & SETTINGS_IMPL::operator=(const SETTINGS_IMPL & rhs)
108 modulesPath = rhs.modulesPath;
109 dirName = rhs.dirName;
110 confDir = rhs.confDir;
111 scriptsDir = rhs.scriptsDir;
113 logFile = rhs.logFile;
114 pidFile = rhs.pidFile;
115 monitorDir = rhs.monitorDir;
116 scriptParams = rhs.scriptParams;
117 monitoring = rhs.monitoring;
118 detailStatWritePeriod = rhs.detailStatWritePeriod;
119 statWritePeriod = rhs.statWritePeriod;
120 stgExecMsgKey = rhs.stgExecMsgKey;
121 executersNum = rhs.executersNum;
122 fullFee = rhs.fullFee;
124 dayResetTraff = rhs.dayResetTraff;
125 spreadFee = rhs.spreadFee;
126 freeMbAllowInet = rhs.freeMbAllowInet;
127 dayFeeIsLastDay = rhs.dayFeeIsLastDay;
128 stopOnError = rhs.stopOnError;
129 writeFreeMbTraffCost = rhs.writeFreeMbTraffCost;
130 showFeeInCash = rhs.showFeeInCash;
131 messageTimeout = rhs.messageTimeout;
132 feeChargeType = rhs.feeChargeType;
133 reconnectOnTariffChange = rhs.reconnectOnTariffChange;
134 disableSessionLog = rhs.disableSessionLog;
135 filterParamsLog = rhs.filterParamsLog;
137 modulesSettings = rhs.modulesSettings;
138 storeModuleSettings = rhs.storeModuleSettings;
141 //-----------------------------------------------------------------------------
142 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
144 const DOTCONFDocumentNode * childNode;
148 pv.param = node->getName();
150 if (node->getValue(1))
152 strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
156 value = node->getValue(0);
160 strError = "Module name expected.";
164 childNode = node->getChildNode();
167 pv.param = childNode->getName();
169 while ((value = childNode->getValue(i++)) != NULL)
171 pv.value.push_back(value);
173 params->push_back(pv);
175 childNode = childNode->getNextNode();
180 //-----------------------------------------------------------------------------
181 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
183 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
184 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
185 settings->logger("%s", buf);
187 //-----------------------------------------------------------------------------
188 int SETTINGS_IMPL::ReadSettings()
190 const char * requiredOptions[] = {
196 "DetailStatWritePeriod",
202 "WriteFreeMbTraffCost",
205 int storeModulesCount = 0;
206 modulesSettings.clear();
208 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
209 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
210 conf.setRequiredOptionNames(requiredOptions);
211 std::string confFile = confDir + "/stargazer.conf";
213 if(conf.setContent(confFile.c_str()) != 0)
215 strError = "Cannot read file " + confFile;
219 const DOTCONFDocumentNode * node = conf.getFirstNode();
223 if (strcasecmp(node->getName(), "ScriptDir") == 0)
225 scriptsDir = node->getValue(0);
228 if (strcasecmp(node->getName(), "LogFile") == 0)
230 logFile = node->getValue(0);
233 if (strcasecmp(node->getName(), "PIDFile") == 0)
235 pidFile = node->getValue(0);
238 if (strcasecmp(node->getName(), "ModulesPath") == 0)
240 modulesPath = node->getValue(0);
243 if (strcasecmp(node->getName(), "Rules") == 0)
245 rules = node->getValue(0);
248 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
250 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
252 strError = "Incorrect DetailStatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
257 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
259 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
261 strError = "Incorrect StatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
266 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
268 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
270 strError = "Incorrect ExecMsgKey value: \'" + std::string(node->getValue(0)) + "\'";
275 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
277 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
279 strError = "Incorrect ExecutersNum value: \'" + std::string(node->getValue(0)) + "\'";
284 if (strcasecmp(node->getName(), "DayFee") == 0)
286 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
288 strError = "Incorrect DayFee value: \'" + std::string(node->getValue(0)) + "\'";
293 if (strcasecmp(node->getName(), "FullFee") == 0)
295 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
297 strError = "Incorrect FullFee value: \'" + std::string(node->getValue(0)) + "\'";
302 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
304 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
306 strError = "Incorrect DayResetTraff value: \'" + std::string(node->getValue(0)) + "\'";
311 if (strcasecmp(node->getName(), "SpreadFee") == 0)
313 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
315 strError = "Incorrect SpreadFee value: \'" + std::string(node->getValue(0)) + "\'";
320 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
322 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
324 strError = "Incorrect FreeMbAllowInet value: \'" + std::string(node->getValue(0)) + "\'";
329 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
331 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
333 strError = "Incorrect DayFeeIsLastDay value: \'" + std::string(node->getValue(0)) + "\'";
338 if (strcasecmp(node->getName(), "StopOnError") == 0)
340 if (ParseYesNo(node->getValue(0), &stopOnError) != 0)
342 strError = "Incorrect StopOnError value: \'" + std::string(node->getValue(0)) + "\'";
347 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
349 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
351 strError = "Incorrect WriteFreeMbTraffCost value: \'" + std::string(node->getValue(0)) + "\'";
356 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
358 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
360 strError = "Incorrect ShowFeeInCash value: \'" + std::string(node->getValue(0)) + "\'";
365 if (strcasecmp(node->getName(), "MonitorDir") == 0)
367 monitorDir = node->getValue(0);
371 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
377 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
379 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
381 strError = "Incorrect MessageTimeout value: \'" + std::string(node->getValue(0)) + "\'";
386 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
388 if (ParseUnsignedInRange(node->getValue(0), 0, 3, &feeChargeType) != 0)
390 strError = "Incorrect FeeChargeType value: \'" + std::string(node->getValue(0)) + "\'";
395 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
397 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
399 strError = "Incorrect ReconnectOnTariffChange value: \'" + std::string(node->getValue(0)) + "\'";
404 if (strcasecmp(node->getName(), "DisableSessionLog") == 0)
406 if (ParseYesNo(node->getValue(0), &disableSessionLog) != 0)
408 strError = "Incorrect DisableSessionLog value: \'" + std::string(node->getValue(0)) + "\'";
413 if (strcasecmp(node->getName(), "FilterParamsLog") == 0)
415 filterParamsLog.clear();
416 for (int i = 0; node->getValue(i) != NULL; ++i)
417 filterParamsLog.push_back(node->getValue(i));
420 if (strcasecmp(node->getName(), "DirNames") == 0)
422 const DOTCONFDocumentNode * child = node->getChildNode();
425 const DOTCONFDocumentNode * dirNameNode;
426 dirName.reserve(DIR_NUM);
427 for (int i = 0; i < DIR_NUM; i++)
430 sprintf(strDirName, "DirName%d", i);
431 dirNameNode = conf.findNode(strDirName, node);
432 if (dirNameNode && dirNameNode->getValue(0))
434 dirName[i] = dirNameNode->getValue(0);
440 if (strcasecmp(node->getName(), "StoreModule") == 0)
442 if (node->getValue(1))
444 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
448 if (storeModulesCount)
450 strError = "Should be only one StoreModule.";
455 storeModuleSettings.moduleName = node->getValue(0);
456 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
459 if (strcasecmp(node->getName(), "Modules") == 0)
461 if (node->getValue(0))
463 strError = "Unexpected \'" + std::string(node->getValue(0)) + "\'.";
466 const DOTCONFDocumentNode * child = node->getChildNode();
469 if (strcasecmp(child->getName(), "Module") != 0)
471 child = child->getNextNode();
474 MODULE_SETTINGS modSettings;
475 modSettings.moduleParams.clear();
476 modSettings.moduleName = child->getValue();
478 ParseModuleSettings(child, &modSettings.moduleParams);
480 modulesSettings.push_back(modSettings);
482 child = child->getNextNode();
486 if (strcasecmp(node->getName(), "ScriptParams") == 0)
488 for (int i = 0; node->getValue(i) != NULL; ++i)
489 scriptParams.push_back(node->getValue(i));
491 node = node->getNextNode();
496 //-----------------------------------------------------------------------------
497 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const std::string & detailStatPeriodStr)
499 if (detailStatPeriodStr == "1")
501 detailStatWritePeriod = dsPeriod_1;
504 else if (detailStatPeriodStr == "1/2")
506 detailStatWritePeriod = dsPeriod_1_2;
509 else if (detailStatPeriodStr == "1/4")
511 detailStatWritePeriod = dsPeriod_1_4;
514 else if (detailStatPeriodStr == "1/6")
516 detailStatWritePeriod = dsPeriod_1_6;
522 //-----------------------------------------------------------------------------