1
1
/**************************************************************************
3
(C) 2008 - 2011 Alexander Shaduri <ashaduri 'at' gmail.com>
3
(C) 2008 - 2012 Alexander Shaduri <ashaduri 'at' gmail.com>
4
4
License: See LICENSE_gsmartcontrol.txt
5
5
***************************************************************************/
7
/// \author Alexander Shaduri
7
12
#ifndef SMARTCTL_PARSER_H
8
13
#define SMARTCTL_PARSER_H
23
/// Note: ALL parse_* functions (except parse_full() and parse_version())
24
/// expect data in unix-newline format!
17
25
class SmartctlParser {
21
30
typedef std::vector<StorageProperty> prop_list_t;
23
// Note: ALL parse_* functions (except parse_full() and parse_version())
24
// expect data in unix-newline format!
27
// Parse full "smartctl -a" output
33
/// Parse full "smartctl -a" output
28
34
bool parse_full(const std::string& s);
31
// supply any output of smartctl here.
37
/// Supply any output of smartctl here, the smartctl version will be retrieved.
32
38
static bool parse_version(const std::string& s, std::string& version, std::string& version_full);
35
// check that the version of smartctl output can be parsed with this parser.
41
/// Check that the version of smartctl output can be parsed with this parser.
36
42
static bool check_version(const std::string& version_str, const std::string& version_full_str);
39
// convert e.g. "1,000,204,886,016 bytes" to 1.00 TiB [931.51 GB, 1000204886016 bytes]
45
/// Convert e.g. "1,000,204,886,016 bytes" to 1.00 TiB [931.51 GB, 1000204886016 bytes]
40
46
static std::string parse_byte_size(const std::string& str, uint64_t& bytes, bool extended);
43
49
// You don't really need to call these functions, use the ones above.
46
// Parse the section part (with "=== .... ===" header) - info or data sections.
52
/// Parse the section part (with "=== .... ===" header) - info or data sections.
47
53
bool parse_section(const std::string& header, const std::string& body);
50
// Parse the info section (without "===" header)
56
/// Parse the info section (without "===" header)
51
57
bool parse_section_info(const std::string& body);
53
// Parse a component (one line) of the info section
59
/// Parse a component (one line) of the info section
54
60
bool parse_section_info_property(StorageProperty& p);
57
// Parse the Data section (without "===" header)
63
/// Parse the Data section (without "===" header)
58
64
bool parse_section_data(const std::string& body);
60
// Parse subsections of Data section
66
/// Parse subsections of Data section
61
67
bool parse_section_data_subsection_health(const std::string& sub);
62
68
bool parse_section_data_subsection_capabilities(const std::string& sub);
63
69
bool parse_section_data_subsection_attributes(const std::string& sub);
97
104
return data_section_data_;
108
/// Get parse error message. Call this only if parsing doesn't succeed,
109
/// to get a friendly error message.
100
110
std::string get_error_msg() const
102
112
return "Cannot parse smartctl output: " + error_msg_;
116
/// Get parse result properties
106
117
const prop_list_t& get_properties() const
108
119
return properties_;
116
// adds a property into property list, looks up and sets its description
127
/// Add a property into property list, look up and set its description
117
128
void add_property(StorageProperty p);
131
/// Set "full" data ("smartctl -a" output)
120
132
void set_data_full(const std::string& s)
138
/// Set "info" section data ("smartctl -i" output, or the first part of "smartctl -a" output)
125
139
void set_data_section_info(const std::string& s)
127
141
data_section_info_ = s;
145
/// Parse "data" section data (the second part of "smartctl -a" output).
130
146
void set_data_section_data(const std::string& s)
132
148
data_section_data_ = s;
152
/// Set error message
136
153
void set_error_msg(const std::string& s)
143
prop_list_t properties_;
145
// These are filled by the appropriate parse_* functions
146
std::string data_full_;
147
std::string data_section_info_;
148
std::string data_section_data_;
150
std::string error_msg_; // on error this will be filled with displayable message
160
prop_list_t properties_; ///< Parsed data properties
162
std::string data_full_; ///< full data, filled by parse_full()
163
std::string data_section_info_; ///< "info" section data, filled by parse_section_info()
164
std::string data_section_data_; ///< "data" section data, filled by parse_section_data()
166
std::string error_msg_; ///< This will be filled with some displayable message on error