Codebase list facter / b3f66d1
(maint) Merge up 31f01b5 to 3.11.x Generated by CI * commit '31f01b5be1f2bc4f73432c9e42200094c013efa5': (packaging) Updating the FACTER.pot file (packaging) Updating the FACTER.pot file (FACT-1504) Use GetTickCount64 as an uptime metric for windows platoforms Jenkins CI 6 years ago
3 changed file(s) with 7 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
1717 {
1818 /**
1919 * Constructs the uptime_resolver.
20 * @param wmi_conn The WMI connection to use when resolving facts.
2120 */
22 uptime_resolver(std::shared_ptr<leatherman::windows::wmi> wmi_conn = std::make_shared<leatherman::windows::wmi>());
21 uptime_resolver();
2322
2423 protected:
2524 /**
2726 * @return Returns the system uptime in seconds.
2827 */
2928 virtual int64_t get_uptime() override;
30
31 private:
32 std::shared_ptr<leatherman::windows::wmi> _wmi;
3329 };
3430
3531 }}} // namespace facter::facts::windows
7474 add(make_shared<windows::memory_resolver>());
7575 add(make_shared<windows::networking_resolver>());
7676 add(make_shared<windows::timezone_resolver>());
77 add(make_shared<windows::uptime_resolver>());
7778
7879 try {
7980 shared_ptr<wmi> shared_wmi = make_shared<wmi>();
8182 add(make_shared<windows::operating_system_resolver>(shared_wmi));
8283 add(make_shared<windows::processor_resolver>(shared_wmi));
8384 add(make_shared<windows::virtualization_resolver>(shared_wmi));
84 add(make_shared<windows::uptime_resolver>(shared_wmi));
8585 } catch (wmi_exception &e) {
8686 LOG_ERROR("failed adding platform facts that require WMI: {1}", e.what());
8787 }
00 #include <internal/facts/windows/uptime_resolver.hpp>
1 #include <leatherman/windows/wmi.hpp>
1 #include <leatherman/windows/windows.hpp>
22 #include <leatherman/util/regex.hpp>
3 #include <leatherman/logging/logging.hpp>
43 #include <leatherman/locale/locale.hpp>
5 #include <boost/date_time/posix_time/posix_time.hpp>
6 #include <boost/date_time/gregorian/gregorian_types.hpp>
74
85 // Mark string for translation (alias for leatherman::locale::format)
96 using leatherman::locale::_;
1310 using namespace std;
1411 using namespace leatherman::util;
1512 using namespace leatherman::windows;
16 using namespace boost::posix_time;
17 using namespace boost::gregorian;
1813
19 uptime_resolver::uptime_resolver(shared_ptr<wmi> wmi_conn) :
20 resolvers::uptime_resolver(),
21 _wmi(move(wmi_conn))
14 uptime_resolver::uptime_resolver() :
15 resolvers::uptime_resolver()
2216 {
23 }
24
25 static ptime get_ptime(string const& wmitime)
26 {
27 static boost::regex wmi_regex("^(\\d{8,})(\\d{2})(\\d{2})(\\d{2})\\.");
28 string iso_date;
29 int hour, min, sec;
30 if (!re_search(wmitime, wmi_regex, &iso_date, &hour, &min, &sec)) {
31 throw runtime_error(_("failed to parse {1} as a date/time", wmitime));
32 }
33
34 return ptime(from_undelimited_string(iso_date), time_duration(hour, min, sec));
3517 }
3618
3719 int64_t uptime_resolver::get_uptime()
3820 {
39 auto vals = _wmi->query(wmi::operatingsystem, {wmi::lastbootuptime, wmi::localdatetime});
40 if (vals.empty()) {
41 return -1;
42 }
43
44 ptime boottime = get_ptime(wmi::get(vals, wmi::lastbootuptime));
45 ptime now = get_ptime(wmi::get(vals, wmi::localdatetime));
46 return (now - boottime).total_seconds();
21 uint64_t tickCount = GetTickCount64();
22 return (int64_t)(tickCount / 1000); // seconds
4723 }
4824
4925 }}} // namespace facter::facts::windows