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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
21 #include "settings_impl.h"
23 #include "stg/logger.h"
24 #include "stg/dotconfpp.h"
25 #include "stg/common.h"
26 #include "stg/const.h"
35 struct Error : public std::runtime_error
37 Error(const std::string& message) : runtime_error(message) {}
40 std::vector<std::string> toValues(const DOTCONFDocumentNode& node)
42 std::vector<std::string> values;
45 const char* value = NULL;
46 while ((value = node.getValue(i++)) != NULL)
47 values.push_back(value);
52 std::vector<PARAM_VALUE> toPVS(const DOTCONFDocumentNode& node)
54 std::vector<PARAM_VALUE> pvs;
56 const DOTCONFDocumentNode* child = node.getChildNode();
59 if (child->getName() == NULL)
62 if (child->getChildNode() == NULL)
63 pvs.push_back(PARAM_VALUE(child->getName(), toValues(*child)));
65 pvs.push_back(PARAM_VALUE(child->getName(), toValues(*child), toPVS(*child)));
67 child = child->getNextNode();
73 unsigned toPeriod(const char* value)
76 throw Error("No detail stat period value.");
78 std::string period(value);
81 else if (period == "1/2")
83 else if (period == "1/4")
85 else if (period == "1/6")
88 throw Error("Invalid detail stat period value: '" + period + "'. Should be one of '1', '1/2', '1/4' or '1/6'.");
93 //-----------------------------------------------------------------------------
94 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
95 : modulesPath("/usr/lib/stg"),
97 confDir(cd.empty() ? "/etc/stargazer" : cd),
99 rules(confDir + "/rules"),
100 logFile("/var/log/stargazer.log"),
101 pidFile("/var/run/stargazer.pid"),
102 monitorDir("/var/stargazer/monitoring"),
104 detailStatWritePeriod(dsPeriod_1_6),
112 freeMbAllowInet(false),
113 dayFeeIsLastDay(false),
115 writeFreeMbTraffCost(false),
119 reconnectOnTariffChange(false),
120 disableSessionLog(false),
121 logger(GetStgLogger())
123 filterParamsLog.push_back("*");
125 //-----------------------------------------------------------------------------
126 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
127 : modulesPath(rval.modulesPath),
128 dirName(rval.dirName),
129 confDir(rval.confDir),
130 scriptsDir(rval.scriptsDir),
132 logFile(rval.logFile),
133 pidFile(rval.pidFile),
134 monitorDir(rval.monitorDir),
135 monitoring(rval.monitoring),
136 detailStatWritePeriod(rval.detailStatWritePeriod),
137 statWritePeriod(rval.statWritePeriod),
138 stgExecMsgKey(rval.stgExecMsgKey),
139 executersNum(rval.executersNum),
140 fullFee(rval.fullFee),
142 dayResetTraff(rval.dayResetTraff),
143 spreadFee(rval.spreadFee),
144 freeMbAllowInet(rval.freeMbAllowInet),
145 dayFeeIsLastDay(rval.dayFeeIsLastDay),
146 stopOnError(rval.stopOnError),
147 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
148 showFeeInCash(rval.showFeeInCash),
149 messageTimeout(rval.messageTimeout),
150 feeChargeType(rval.feeChargeType),
151 reconnectOnTariffChange(rval.reconnectOnTariffChange),
152 disableSessionLog(rval.disableSessionLog),
153 filterParamsLog(rval.filterParamsLog),
154 modulesSettings(rval.modulesSettings),
155 storeModuleSettings(rval.storeModuleSettings),
156 logger(GetStgLogger())
159 //-----------------------------------------------------------------------------
160 SETTINGS_IMPL & SETTINGS_IMPL::operator=(const SETTINGS_IMPL & rhs)
162 modulesPath = rhs.modulesPath;
163 dirName = rhs.dirName;
164 confDir = rhs.confDir;
165 scriptsDir = rhs.scriptsDir;
167 logFile = rhs.logFile;
168 pidFile = rhs.pidFile;
169 monitorDir = rhs.monitorDir;
170 scriptParams = rhs.scriptParams;
171 monitoring = rhs.monitoring;
172 detailStatWritePeriod = rhs.detailStatWritePeriod;
173 statWritePeriod = rhs.statWritePeriod;
174 stgExecMsgKey = rhs.stgExecMsgKey;
175 executersNum = rhs.executersNum;
176 fullFee = rhs.fullFee;
178 dayResetTraff = rhs.dayResetTraff;
179 spreadFee = rhs.spreadFee;
180 freeMbAllowInet = rhs.freeMbAllowInet;
181 dayFeeIsLastDay = rhs.dayFeeIsLastDay;
182 stopOnError = rhs.stopOnError;
183 writeFreeMbTraffCost = rhs.writeFreeMbTraffCost;
184 showFeeInCash = rhs.showFeeInCash;
185 messageTimeout = rhs.messageTimeout;
186 feeChargeType = rhs.feeChargeType;
187 reconnectOnTariffChange = rhs.reconnectOnTariffChange;
188 disableSessionLog = rhs.disableSessionLog;
189 filterParamsLog = rhs.filterParamsLog;
191 modulesSettings = rhs.modulesSettings;
192 storeModuleSettings = rhs.storeModuleSettings;
195 //-----------------------------------------------------------------------------
196 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
198 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
199 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
200 settings->logger("%s", buf);
202 //-----------------------------------------------------------------------------
203 int SETTINGS_IMPL::ReadSettings()
205 const char * requiredOptions[] = {
211 "DetailStatWritePeriod",
217 "WriteFreeMbTraffCost",
220 int storeModulesCount = 0;
221 modulesSettings.clear();
223 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
224 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
225 conf.setRequiredOptionNames(requiredOptions);
226 std::string confFile = confDir + "/stargazer.conf";
228 if(conf.setContent(confFile.c_str()) != 0)
230 strError = "Cannot read file " + confFile;
234 const DOTCONFDocumentNode * node = conf.getFirstNode();
238 if (strcasecmp(node->getName(), "ScriptDir") == 0)
240 scriptsDir = node->getValue(0);
243 if (strcasecmp(node->getName(), "LogFile") == 0)
245 logFile = node->getValue(0);
248 if (strcasecmp(node->getName(), "PIDFile") == 0)
250 pidFile = node->getValue(0);
253 if (strcasecmp(node->getName(), "ModulesPath") == 0)
255 modulesPath = node->getValue(0);
258 if (strcasecmp(node->getName(), "Rules") == 0)
260 rules = node->getValue(0);
263 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
267 detailStatWritePeriod = toPeriod(node->getValue(0));
269 catch (const Error& error)
271 strError = error.what();
276 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
278 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
280 strError = "Incorrect StatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
285 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
287 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
289 strError = "Incorrect ExecMsgKey value: \'" + std::string(node->getValue(0)) + "\'";
294 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
296 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
298 strError = "Incorrect ExecutersNum value: \'" + std::string(node->getValue(0)) + "\'";
303 if (strcasecmp(node->getName(), "DayFee") == 0)
305 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
307 strError = "Incorrect DayFee value: \'" + std::string(node->getValue(0)) + "\'";
312 if (strcasecmp(node->getName(), "FullFee") == 0)
314 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
316 strError = "Incorrect FullFee value: \'" + std::string(node->getValue(0)) + "\'";
321 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
323 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
325 strError = "Incorrect DayResetTraff value: \'" + std::string(node->getValue(0)) + "\'";
330 if (strcasecmp(node->getName(), "SpreadFee") == 0)
332 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
334 strError = "Incorrect SpreadFee value: \'" + std::string(node->getValue(0)) + "\'";
339 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
341 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
343 strError = "Incorrect FreeMbAllowInet value: \'" + std::string(node->getValue(0)) + "\'";
348 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
350 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
352 strError = "Incorrect DayFeeIsLastDay value: \'" + std::string(node->getValue(0)) + "\'";
357 if (strcasecmp(node->getName(), "StopOnError") == 0)
359 if (ParseYesNo(node->getValue(0), &stopOnError) != 0)
361 strError = "Incorrect StopOnError value: \'" + std::string(node->getValue(0)) + "\'";
366 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
368 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
370 strError = "Incorrect WriteFreeMbTraffCost value: \'" + std::string(node->getValue(0)) + "\'";
375 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
377 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
379 strError = "Incorrect ShowFeeInCash value: \'" + std::string(node->getValue(0)) + "\'";
384 if (strcasecmp(node->getName(), "MonitorDir") == 0)
386 monitorDir = node->getValue(0);
390 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
396 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
398 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
400 strError = "Incorrect MessageTimeout value: \'" + std::string(node->getValue(0)) + "\'";
405 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
407 if (ParseUnsignedInRange(node->getValue(0), 0, 3, &feeChargeType) != 0)
409 strError = "Incorrect FeeChargeType value: \'" + std::string(node->getValue(0)) + "\'";
414 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
416 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
418 strError = "Incorrect ReconnectOnTariffChange value: \'" + std::string(node->getValue(0)) + "\'";
423 if (strcasecmp(node->getName(), "DisableSessionLog") == 0)
425 if (ParseYesNo(node->getValue(0), &disableSessionLog) != 0)
427 strError = "Incorrect DisableSessionLog value: \'" + std::string(node->getValue(0)) + "\'";
432 if (strcasecmp(node->getName(), "FilterParamsLog") == 0)
434 filterParamsLog.clear();
435 for (int i = 0; node->getValue(i) != NULL; ++i)
436 filterParamsLog.push_back(node->getValue(i));
439 if (strcasecmp(node->getName(), "DirNames") == 0)
441 const DOTCONFDocumentNode * child = node->getChildNode();
444 const DOTCONFDocumentNode * dirNameNode;
445 dirName.reserve(DIR_NUM);
446 for (int i = 0; i < DIR_NUM; i++)
449 sprintf(strDirName, "DirName%d", i);
450 dirNameNode = conf.findNode(strDirName, node);
451 if (dirNameNode && dirNameNode->getValue(0))
453 dirName[i] = dirNameNode->getValue(0);
459 if (strcasecmp(node->getName(), "StoreModule") == 0)
461 if (node->getValue(1))
463 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
467 if (storeModulesCount)
469 strError = "Should be only one StoreModule.";
474 if (node->getValue(0) == NULL)
476 strError = "No module name in the StoreModule section.";
479 storeModuleSettings.moduleName = node->getValue(0);
480 storeModuleSettings.moduleParams = toPVS(*node);
483 if (strcasecmp(node->getName(), "Modules") == 0)
485 if (node->getValue(0))
487 strError = "Unexpected \'" + std::string(node->getValue(0)) + "\'.";
490 const DOTCONFDocumentNode * child = node->getChildNode();
493 if (strcasecmp(child->getName(), "Module") != 0)
495 child = child->getNextNode();
499 if (child->getValue(0) == NULL)
501 strError = "No module name in the Module section.";
505 modulesSettings.push_back(MODULE_SETTINGS(child->getValue(0), toPVS(*child)));
507 child = child->getNextNode();
511 if (strcasecmp(node->getName(), "ScriptParams") == 0)
513 for (int i = 0; node->getValue(i) != NULL; ++i)
514 scriptParams.push_back(node->getValue(i));
516 node = node->getNextNode();
521 //-----------------------------------------------------------------------------