37 #include <boost/lexical_cast.hpp> 38 #include <boost/format.hpp> 40 #include <boost/date_time/posix_time/posix_time.hpp> 49 const std::string procCPUInfo =
"/proc/cpuinfo";
50 std::ifstream stream(procCPUInfo.c_str());
51 stream >> std::noskipws;
53 std::copy(std::istream_iterator<char>(stream),
54 std::istream_iterator<char>(),
55 std::back_inserter(content));
57 std::string pattern(
"model name : ");
58 std::string::iterator start = std::search(content.begin(), content.end(),
59 pattern.begin(), pattern.end());
60 if (start == content.end()) {
61 throw std::runtime_error(
62 boost::str(boost::format(
"Could not determine machine version " 63 "since the \"%1\" entry could not be " 64 "found in the \"%2%\" file.")
65 % pattern % procCPUInfo));
68 std::string::iterator end = std::find(start, content.end(),
'\n');
69 if (end == content.end()) {
70 throw std::runtime_error(
71 boost::str(boost::format(
"Could not determine machine version " 72 "since the end of the \"%1\" entry " 73 "could not be found in the \"%2%\" " 75 % pattern % procCPUInfo));
78 std::copy(start + pattern.size(), end, std::back_inserter(value));
84 std::string
tryFile(
const std::string& filename) {
85 std::string result =
"";
86 std::ifstream stream(filename.c_str());
94 std::ostringstream stream;
95 stream << std::hex << gethostid();
101 if (!(result =
tryFile(
"/etc/machine-id")).empty()) {
103 }
else if (!(result =
tryFile(
"/var/lib/dbus/machine-id")).empty()) {
113 boost::uint32_t bootTimeUNIXSeconds;
115 const std::string procStat =
"/proc/stat";
116 std::ifstream stream(procStat.c_str());
117 stream >> std::noskipws;
119 std::copy(std::istream_iterator<char>(stream),
120 std::istream_iterator<char>() ,
121 std::back_inserter(content));
122 std::string pattern(
"btime ");
123 std::string::iterator start = std::search(content.begin(), content.end(),
124 pattern.begin(), pattern.end());
125 if (start == content.end()) {
126 throw std::runtime_error(
127 boost::str(boost::format(
"Could not determine the boot time " 128 "since the \"%1\" entry could not be " 129 "found in the \"%2%\" file.")
130 % pattern % procStat));
132 std::string::iterator end = std::find(start, content.end(),
'\n');
133 if (end == content.end()) {
134 throw std::runtime_error(
135 boost::str(boost::format(
"Could not determine the boot time " 136 "since the end of the \"%1\" entry " 137 "could not be found in the \"%2%\" " 139 % pattern % procStat));
142 std::copy(start + pattern.size(), end, std::back_inserter(value));
144 bootTimeUNIXSeconds = boost::lexical_cast<boost::int32_t>(value);
145 }
catch (
const boost::bad_lexical_cast& e) {
146 throw std::runtime_error(
147 boost::str(boost::format(
"Could not determine the boot time " 148 "since the \"%1\" entry in the " 149 "\"%2%\" file could not be parsed. " 151 % pattern % procStat % e.what()));
154 return boost::posix_time::from_time_t(bootTimeUNIXSeconds);
RSC_EXPORT std::string currentHostId()
Determine and return a unique id string of the local machine.
RSC_EXPORT boost::posix_time::ptime currentBootTime()
Return the boot time of the local machine.
RSC_EXPORT std::string currentMachineVersion()
Determine and return the version within its type, usually the CPU identification string, of the local machine.
std::string tryFile(const std::string &filename)