39 if (now-fLastUpdate<boost::posix_time::minutes(fInterval))
44 const string cmd =
"ntpdate -q "+fServer;
46 Info(
"Calling '"+cmd+
"'");
49 FILE *pipe = popen(cmd.c_str(),
"r");
52 const string err = strerror(errno);
53 Error(
"Could not create pipe '"+cmd+
"': "+err);
62 const int rc = fgetc(pipe);
63 if (rc==EOF || rc==
'\n')
76 line +=
static_cast<unsigned char>(rc);
82 Error(
"First returned line contains other than four arguments (separated by commas)");
86 if (args[2].substr(0, 7)!=
"offset ")
88 Error(
"Argument 3 '"+args[2]+
"' is not what it ought to be.");
94 const float offset = stof(args[2].substr(7));
97 const string msg =
"NTP: "+fServer+
" returned: "+args[2]+
" ms";
102 return kStateOutOfRange;
108 catch (
const exception &e)
110 Error(
"Converting offset '"+args[2]+
"' to float failed: "+e.what());
114 return kStateRunning;
119 fLastUpdate =
Time()-boost::posix_time::minutes(fInterval);
120 return GetCurrentState();
125 fService(
"TIME_CHECK/OFFSET",
"F:1",
"Time offset measured with ntp|offset[ms]:Time offset in milliseconds")
128 AddStateName(kStateRunning,
"Valid",
"Last check was valid.");
129 AddStateName(kStateOutOfRange,
"OutOfRange",
"Last time check exceeded 1s.");
138 fServer = conf.
Get<
string>(
"ntp-server");
139 fInterval = conf.
Get<uint16_t>(
"interval");
156 return Main::execute<T, StateMachineTimeCheck>(conf);
161 po::options_description control(
"Time check");
162 control.add_options()
163 (
"ntp-server", var<string>(
"hora.roa.es"),
"The ntp server to be queried")
164 (
"interval", var<uint16_t>(15),
"Interval in minutes the ntp server should be queried")
197 Main::PrintHelp<StateMachineTimeCheck>();
217 int main(
int argc,
const char* argv[])
227 if (!conf.
Has(
"console"))
228 return RunShell<LocalStream>(conf);
230 if (conf.
Get<
int>(
"console")==0)
231 return RunShell<LocalShell>(conf);
233 return RunShell<LocalConsole>(conf);
int Execute()
Is called continously to execute actions in the current state.
StateMachineTimeCheck(ostream &out=cout)
void SetupConfiguration(Configuration &conf)
Adds some functionality to boost::posix_time::ptime for our needs.
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
int EvalOptions(Configuration &conf)
bool Has(const std::string &var)
void AddOptions(const po::options_description &opt, bool visible=true)
DimDescribedService fService
Commandline parsing, resource file parsing and database access.
int main(int argc, const char *argv[])
Class for a state machine implementation within a DIM network.
void SetupConfiguration(Configuration &conf)
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
int RunShell(Configuration &conf)