Improve error messages for the check result reader.

Fixes #5275
This commit is contained in:
Gunnar Beutner 2013-12-09 09:52:09 +01:00
parent 3b99dc1e84
commit 203cf73bf7
2 changed files with 31 additions and 5 deletions

View File

@ -26,6 +26,8 @@
#include "base/convert.h"
#include "base/application.h"
#include "base/utility.h"
#include "base/exception.h"
#include "base/context.h"
#include <fstream>
using namespace icinga;
@ -48,11 +50,15 @@ void CheckResultReader::Start(void)
*/
void CheckResultReader::ReadTimerHandler(void) const
{
CONTEXT("Processing check result files in '" + GetSpoolDir() + "'");
Utility::Glob(GetSpoolDir() + "/c??????.ok", boost::bind(&CheckResultReader::ProcessCheckResultFile, this, _1));
}
void CheckResultReader::ProcessCheckResultFile(const String& path) const
{
CONTEXT("Processing check result file '" + path + "'");
String crfile = String(path.Begin(), path.End() - 3); /* Remove the ".ok" extension. */
std::ifstream fp;
@ -80,8 +86,17 @@ void CheckResultReader::ProcessCheckResultFile(const String& path) const
}
/* Remove the checkresult files. */
(void)unlink(path.CStr());
(void)unlink(crfile.CStr());
if (unlink(path.CStr()) < 0)
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("unlink")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(path));
if (unlink(crfile.CStr()) < 0)
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("unlink")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(crfile));
Host::Ptr host = Host::GetByName(attrs["host_name"]);
@ -120,4 +135,3 @@ void CheckResultReader::ProcessCheckResultFile(const String& path) const
service->SetNextCheck(Utility::GetTime() + service->GetCheckInterval());
}
}

View File

@ -38,13 +38,25 @@ public:
template<typename T>
static long ToLong(const T& val)
{
return boost::lexical_cast<long>(val);
try {
return boost::lexical_cast<long>(val);
} catch (std::exception&) {
std::ostringstream msgbuf;
msgbuf << "Can't convert '" << val << "' to an integer.";
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
}
}
template<typename T>
static double ToDouble(const T& val)
{
return boost::lexical_cast<double>(val);
try {
return boost::lexical_cast<double>(val);
} catch (std::exception&) {
std::ostringstream msgbuf;
msgbuf << "Can't convert '" << val << "' to a floating point number.";
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
}
}
static bool ToBool(const String& val);