1faf33367SBen Tyner #include <util/ffdc_file.hpp>
2faf33367SBen Tyner #include <util/trace.hpp>
3faf33367SBen Tyner
4faf33367SBen Tyner #include <string>
5faf33367SBen Tyner #include <vector>
6faf33367SBen Tyner
7faf33367SBen Tyner namespace util
8faf33367SBen Tyner {
9faf33367SBen Tyner
10faf33367SBen Tyner /**
11faf33367SBen Tyner * Parse systemd journal message field
12faf33367SBen Tyner *
13faf33367SBen Tyner * Parse the journal looking for the specified field and return the journal
14faf33367SBen Tyner * data for that field.
15faf33367SBen Tyner *
16faf33367SBen Tyner * @param journal - The journal to parse
17faf33367SBen Tyner * @param field - Field containing the data to retrieve
18faf33367SBen Tyner * @return Data for the speciefied field
19faf33367SBen Tyner */
sdjGetFieldValue(sd_journal * journal,const char * field)20faf33367SBen Tyner std::string sdjGetFieldValue(sd_journal* journal, const char* field)
21faf33367SBen Tyner {
22faf33367SBen Tyner const char* data{nullptr};
23faf33367SBen Tyner size_t length{0};
24faf33367SBen Tyner
25faf33367SBen Tyner // get field value
26faf33367SBen Tyner if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
27faf33367SBen Tyner {
28faf33367SBen Tyner size_t prefix{0};
29faf33367SBen Tyner
30faf33367SBen Tyner // The data returned by sd_journal_get_data will be prefixed with the
31faf33367SBen Tyner // field name and "="
32faf33367SBen Tyner const void* eq = memchr(data, '=', length);
33faf33367SBen Tyner if (nullptr != eq)
34faf33367SBen Tyner {
35faf33367SBen Tyner // get just data following the "="
36faf33367SBen Tyner prefix = (const char*)eq - data + 1;
37faf33367SBen Tyner }
38faf33367SBen Tyner else
39faf33367SBen Tyner {
40faf33367SBen Tyner // all the data (should not happen)
41faf33367SBen Tyner prefix = 0;
42faf33367SBen Tyner std::string value{}; // empty string
43faf33367SBen Tyner }
44faf33367SBen Tyner
45faf33367SBen Tyner return std::string{data + prefix, length - prefix};
46faf33367SBen Tyner }
47faf33367SBen Tyner else
48faf33367SBen Tyner {
49faf33367SBen Tyner return std::string{}; // empty string
50faf33367SBen Tyner }
51faf33367SBen Tyner }
52faf33367SBen Tyner
53faf33367SBen Tyner /**
54faf33367SBen Tyner * Gather messages from the journal
55faf33367SBen Tyner *
56faf33367SBen Tyner * Fetch journal entry data for all entries with the specified field equal to
57faf33367SBen Tyner * the specified value.
58faf33367SBen Tyner *
59faf33367SBen Tyner * @param field - Field to search on
60faf33367SBen Tyner * @param fieldValue - Value to search for
61faf33367SBen Tyner * @param max - Maximum number of messages fetch
62faf33367SBen Tyner * @return Vector of journal entry data
63faf33367SBen Tyner */
sdjGetMessages(const std::string & field,const std::string & fieldValue,unsigned int max)64*a0c724d3SPatrick Williams std::vector<std::string> sdjGetMessages(
65*a0c724d3SPatrick Williams const std::string& field, const std::string& fieldValue, unsigned int max)
66faf33367SBen Tyner {
67faf33367SBen Tyner sd_journal* journal;
68faf33367SBen Tyner std::vector<std::string> messages;
69faf33367SBen Tyner
70faf33367SBen Tyner if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
71faf33367SBen Tyner {
72faf33367SBen Tyner SD_JOURNAL_FOREACH_BACKWARDS(journal)
73faf33367SBen Tyner {
74faf33367SBen Tyner // Get input field
75faf33367SBen Tyner std::string value = sdjGetFieldValue(journal, field.c_str());
76faf33367SBen Tyner
77faf33367SBen Tyner // Compare field value and read data
78faf33367SBen Tyner if (value == fieldValue)
79faf33367SBen Tyner {
80faf33367SBen Tyner // Get SYSLOG_IDENTIFIER field (process that logged message)
81*a0c724d3SPatrick Williams std::string syslog =
82*a0c724d3SPatrick Williams sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
83faf33367SBen Tyner
84faf33367SBen Tyner // Get _PID field
85faf33367SBen Tyner std::string pid = sdjGetFieldValue(journal, "_PID");
86faf33367SBen Tyner
87faf33367SBen Tyner // Get MESSAGE field
88faf33367SBen Tyner std::string message = sdjGetFieldValue(journal, "MESSAGE");
89faf33367SBen Tyner
90faf33367SBen Tyner // Get timestamp
91faf33367SBen Tyner uint64_t usec{0};
92faf33367SBen Tyner if (0 == sd_journal_get_realtime_usec(journal, &usec))
93faf33367SBen Tyner {
94faf33367SBen Tyner // Convert realtime microseconds to date format
95faf33367SBen Tyner char dateBuffer[80];
96faf33367SBen Tyner std::string date;
97faf33367SBen Tyner std::time_t timeInSecs = usec / 1000000;
98faf33367SBen Tyner strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
99faf33367SBen Tyner std::localtime(&timeInSecs));
100faf33367SBen Tyner date = dateBuffer;
101faf33367SBen Tyner
102faf33367SBen Tyner // Store value to messages
103faf33367SBen Tyner value = date + " " + syslog + "[" + pid + "]: " + message;
104faf33367SBen Tyner messages.insert(messages.begin(), value);
105faf33367SBen Tyner }
106faf33367SBen Tyner }
107faf33367SBen Tyner
108faf33367SBen Tyner // limit maximum number of messages
109faf33367SBen Tyner if (messages.size() >= max)
110faf33367SBen Tyner {
111faf33367SBen Tyner break;
112faf33367SBen Tyner }
113faf33367SBen Tyner }
114faf33367SBen Tyner
115faf33367SBen Tyner sd_journal_close(journal); // close journal when done
116faf33367SBen Tyner }
117faf33367SBen Tyner
118faf33367SBen Tyner return messages;
119faf33367SBen Tyner }
120faf33367SBen Tyner /**
121faf33367SBen Tyner * @brief Create an FFDCFile object containing the specified lines of text data
122faf33367SBen Tyner *
123faf33367SBen Tyner * Throws an exception if an error occurs.
124faf33367SBen Tyner *
125faf33367SBen Tyner * @param lines - lines of text data to write to file
126faf33367SBen Tyner * @return FFDCFile object
127faf33367SBen Tyner */
createFFDCTraceFile(const std::vector<std::string> & lines)128faf33367SBen Tyner FFDCFile createFFDCTraceFile(const std::vector<std::string>& lines)
129faf33367SBen Tyner {
130faf33367SBen Tyner // Create FFDC file of type Text
131faf33367SBen Tyner FFDCFile file{FFDCFormat::Text};
132faf33367SBen Tyner int fd = file.getFileDescriptor();
133faf33367SBen Tyner
134faf33367SBen Tyner // Write FFDC lines to file
135faf33367SBen Tyner std::string buffer;
136faf33367SBen Tyner for (const std::string& line : lines)
137faf33367SBen Tyner {
138faf33367SBen Tyner // Copy line to buffer. Add newline if necessary.
139faf33367SBen Tyner buffer = line;
140faf33367SBen Tyner if (line.empty() || (line.back() != '\n'))
141faf33367SBen Tyner {
142faf33367SBen Tyner buffer += '\n';
143faf33367SBen Tyner }
144faf33367SBen Tyner
145faf33367SBen Tyner // write buffer to file
146faf33367SBen Tyner size_t numBytes = write(fd, buffer.c_str(), buffer.size());
147faf33367SBen Tyner if (buffer.size() != numBytes)
148faf33367SBen Tyner {
149faf33367SBen Tyner trace::err("%s only %u of %u bytes written", file.getPath().c_str(),
150faf33367SBen Tyner numBytes, buffer.size());
151faf33367SBen Tyner }
152faf33367SBen Tyner }
153faf33367SBen Tyner
154faf33367SBen Tyner // Seek to beginning of file so error logging system can read data
155faf33367SBen Tyner lseek(fd, 0, SEEK_SET);
156faf33367SBen Tyner
157faf33367SBen Tyner return file;
158faf33367SBen Tyner }
159faf33367SBen Tyner
160faf33367SBen Tyner /**
161faf33367SBen Tyner * Create FDDC files from journal messages of relevant executables
162faf33367SBen Tyner *
163faf33367SBen Tyner * Parse the system journal looking for log entries created by the executables
164faf33367SBen Tyner * of interest for logging. For each of these entries create a ffdc trace file
165faf33367SBen Tyner * that will be used to create ffdc log entries. These files will be pushed
166faf33367SBen Tyner * onto the stack of ffdc files.
167faf33367SBen Tyner *
168faf33367SBen Tyner * @param i_files - vector of ffdc files that will become log entries
169faf33367SBen Tyner */
createFFDCTraceFiles(std::vector<FFDCFile> & i_files)170faf33367SBen Tyner void createFFDCTraceFiles(std::vector<FFDCFile>& i_files)
171faf33367SBen Tyner {
172faf33367SBen Tyner // Executables of interest
173faf33367SBen Tyner std::vector<std::string> executables{"openpower-hw-diags"};
174faf33367SBen Tyner
175faf33367SBen Tyner for (const std::string& executable : executables)
176faf33367SBen Tyner {
177faf33367SBen Tyner try
178faf33367SBen Tyner {
179faf33367SBen Tyner // get journal messages
180faf33367SBen Tyner std::vector<std::string> messages =
181faf33367SBen Tyner sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30);
182faf33367SBen Tyner
183faf33367SBen Tyner // Create FFDC file containing the journal messages
184faf33367SBen Tyner if (!messages.empty())
185faf33367SBen Tyner {
186faf33367SBen Tyner i_files.emplace_back(createFFDCTraceFile(messages));
187faf33367SBen Tyner }
188faf33367SBen Tyner }
189faf33367SBen Tyner catch (const std::exception& e)
190faf33367SBen Tyner {
191faf33367SBen Tyner trace::inf("createFFDCTraceFiles exception");
192faf33367SBen Tyner trace::inf(e.what());
193faf33367SBen Tyner }
194faf33367SBen Tyner }
195faf33367SBen Tyner }
196faf33367SBen Tyner
197faf33367SBen Tyner } // namespace util
198