Skip to content

Commit

Permalink
[ARMT] added scheduler and run loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey McRae committed Nov 4, 2012
1 parent 620c734 commit 7edbc37
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ OBJECTS += common/CProcInfo.o
OBJECTS += common/CPCIInfo.o
OBJECTS += common/CHTTP.o
OBJECTS += common/CMessageBuilder.o
OBJECTS += common/CScheduler.o

OBJECTS += block/CBlockEnumerator.o
OBJECTS += block/CSMARTBlockDevice.o
Expand Down
59 changes: 56 additions & 3 deletions armt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/utsname.h>
#include <time.h>

#include <iostream>
#include <fstream>
Expand All @@ -33,6 +34,7 @@
#include "common/CHTTP.h"
#include "common/CCompress.h"
#include "common/CMessageBuilder.h"
#include "common/CScheduler.h"

#include "fs/CFSVerifier.h"
#include "block/CBlockEnumerator.h"
Expand Down Expand Up @@ -104,6 +106,39 @@ bool FSCHECK(std::iostream &ss)
return fs.Save(ss);
}

class CMSGJob: public ISchedulerJob
{
public:
CMSGJob(
const std::time_t next ,
const unsigned int interval,
CMessageBuilder *msg ,
const std::string &name ,
CMessageBuilder::SegmentFn fn
) :
m_next (next ),
m_interval(interval),
m_msg (msg ),
m_name (name ),
m_fn (fn )
{}

virtual std::time_t GetRunTime( ) { return m_next; }
virtual void SetRunTime(std::time_t time) { m_next = time; }
virtual unsigned int GetDelayInterval( ) { return m_interval; }
virtual void Execute()
{
m_msg->AppendSegment(m_name, m_fn);
}

private:
std::time_t m_next;
unsigned int m_interval;
CMessageBuilder *m_msg;
std::string m_name;
CMessageBuilder::SegmentFn m_fn;
};

int main(int argc, char *argv[])
{
/* must be called first */
Expand Down Expand Up @@ -134,10 +169,28 @@ int main(int argc, char *argv[])
return -1;
}


CMessageBuilder msg(armthost, armtport);
msg.AppendSegment("DISKCHECK", &DISKCHECK);
msg.AppendSegment("FSCHECK" , &FSCHECK );
msg.Send();
CScheduler s;

/* calculate the GMT time for midnight tonight in the server's timezone */
tzset();
std::time_t midnight = time(NULL);
midnight -= midnight % 86400;
midnight += 86400;
midnight += timezone - (daylight * 3600);

/* add the jobs to the scheduler */
s.AddJob(new CMSGJob(midnight , 86400, &msg, "FSCHECK" , &FSCHECK ));
s.AddJob(new CMSGJob(time(NULL), 60 , &msg, "DISKCHECK", &DISKCHECK));

while(true)
{
msg.Reset();
if (s.Run())
msg.Send();
sleep(1);
}

if (_hostent)
DNS.Free(_hostent);
Expand Down
10 changes: 10 additions & 0 deletions common/CMessageBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ void CMessageBuilder::AppendSegment(const std::string &name, SegmentFn fn)
m_segments[name] = fn;
}

void CMessageBuilder::Reset()
{
m_segments.clear();
}

void CMessageBuilder::PackString(std::ostream &ss, const std::string &value)
{
uint16_t len = value.length();
Expand All @@ -190,6 +195,7 @@ bool CMessageBuilder::Send()
{
std::string body;
{
bool send = false;
std::stringstream total;
for(SegmentList::iterator segment = m_segments.begin(); segment != m_segments.end(); ++segment)
{
Expand All @@ -207,8 +213,12 @@ bool CMessageBuilder::Send()
total.write((const char *)&namelen, sizeof(namelen));
total.write((const char *)&datalen, sizeof(datalen));
total << segment->first << ss.str();
send = true;
}

/* if there is nothing to send, do not do anything */
if (!send)
return true;

std::stringstream compressed;
CCompress::Deflate(total, compressed);
Expand Down
1 change: 1 addition & 0 deletions common/CMessageBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CMessageBuilder
void InitAuth();

void AppendSegment(const std::string &name, SegmentFn fn);
void Reset();
bool Send();

static void PackString(std::ostream &ss, const std::string &value);
Expand Down
58 changes: 58 additions & 0 deletions common/CScheduler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* ARMT (Another Remote Monitoring Tool)
* Copyright (C) Geoffrey McRae 2012 <geoff@spacevs.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "CScheduler.h"

CScheduler::CScheduler()
{
}

CScheduler::~CScheduler()
{
for(JobList::iterator it = m_jobs.begin(); it != m_jobs.end(); ++it)
{
ISchedulerJob *job = *it;
delete job;
}
}

bool CScheduler::Run()
{
std::time_t currentTime = std::time(NULL);
bool ran = false;

for(JobList::iterator it = m_jobs.begin(); it != m_jobs.end(); ++it)
{
ISchedulerJob *job = *it;
if (job->GetRunTime() > currentTime)
continue;

ran = true;
job->Execute();
job->SetRunTime(job->GetRunTime() + job->GetDelayInterval());
}

return ran;
}

void CScheduler::AddJob(ISchedulerJob *job)
{
m_jobs.push_back(job);
}
63 changes: 63 additions & 0 deletions common/CScheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* ARMT (Another Remote Monitoring Tool)
* Copyright (C) Geoffrey McRae 2012 <geoff@spacevs.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#ifndef _CSCHEDULER_H_
#define _CSCHEDULER_H_

#include <ctime>
#include <vector>

class ISchedulerJob
{
public:
ISchedulerJob() {}
virtual ~ISchedulerJob() {}

virtual std::time_t GetRunTime() = 0;
virtual void SetRunTime(std::time_t time) = 0;
virtual unsigned int GetDelayInterval() = 0;
virtual void Execute() = 0;
};

class CScheduler
{
public:
CScheduler();
~CScheduler();

/**
* Run's any jobs that need running
* @return True if any jobs ran
*/
bool Run();

/**
* Add a recurring job to execute at the specified time
* @param job An instance of ISchedulerJob
*/
void AddJob(ISchedulerJob *job);

private:
typedef std::vector<ISchedulerJob *> JobList;

JobList m_jobs;
};

#endif // _CSCHEDULER_H_

0 comments on commit 7edbc37

Please sign in to comment.