-
Notifications
You must be signed in to change notification settings - Fork 12
/
hwmon_ffdc.cpp
137 lines (113 loc) · 3.06 KB
/
hwmon_ffdc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "hwmon_ffdc.hpp"
#include "logging.hpp"
#include <array>
#include <filesystem>
#include <format>
#include <fstream>
#include <string>
#include <vector>
namespace phosphor::fan::monitor
{
namespace util
{
namespace fs = std::filesystem;
inline std::vector<std::string> executeCommand(const std::string& command)
{
std::vector<std::string> output;
std::array<char, 128> buffer;
auto pipe_close = [](auto fd) { (void)pclose(fd); };
std::unique_ptr<FILE, decltype(pipe_close)> pipe(
popen(command.c_str(), "r"), pipe_close);
if (!pipe)
{
getLogger().log(
std::format("popen() failed when running command: {}", command));
return output;
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
output.emplace_back(buffer.data());
}
return output;
}
std::vector<std::string> getHwmonNameFFDC()
{
const fs::path hwmonBaseDir{"/sys/class/hwmon"};
std::vector<std::string> hwmonNames;
if (!fs::exists(hwmonBaseDir))
{
getLogger().log(std::format("Hwmon base directory {} doesn't exist",
hwmonBaseDir.native()));
return hwmonNames;
}
try
{
for (const auto& path : fs::directory_iterator(hwmonBaseDir))
{
if (!path.is_directory())
{
continue;
}
auto nameFile = path.path() / "name";
if (fs::exists(nameFile))
{
std::ifstream f{nameFile};
if (f.good())
{
std::string name;
f >> name;
hwmonNames.push_back(name);
}
}
}
}
catch (const std::exception& e)
{
getLogger().log(
std::format("Error traversing hwmon directories: {}", e.what()));
}
return hwmonNames;
}
std::vector<std::string> getDmesgFFDC()
{
std::vector<std::string> output;
auto dmesgOutput = executeCommand("dmesg");
// Only pull in dmesg lines with interesting keywords.
// One example is:
// [ 16.390603] max31785: probe of 7-0052 failed with error -110
// using ' probe' to avoid 'modprobe'
std::vector<std::string> matches{" probe", "failed"};
for (const auto& line : dmesgOutput)
{
for (const auto& m : matches)
{
if (line.find(m) != std::string::npos)
{
output.push_back(line);
if (output.back().back() == '\n')
{
output.back().pop_back();
}
break;
}
}
}
return output;
}
} // namespace util
nlohmann::json collectHwmonFFDC()
{
nlohmann::json ffdc;
auto hwmonNames = util::getHwmonNameFFDC();
if (!hwmonNames.empty())
{
ffdc["hwmonNames"] = std::move(hwmonNames);
}
auto dmesg = util::getDmesgFFDC();
if (!dmesg.empty())
{
ffdc["dmesg"] = std::move(dmesg);
}
return ffdc;
}
} // namespace phosphor::fan::monitor