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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::error::MetricsResult;
use crate::ir::{TsPoint, TsValue};
use std::collections::HashMap;
use std::str::FromStr;
#[derive(Deserialize, Debug)]
pub struct TelegrafConfig {
pub endpoints: Vec<String>,
pub port: u64,
pub user: String,
pub password: String,
pub region: String,
}
#[test]
fn test_telegraf_parsing() {
use std::fs::File;
use std::io::Read;
let mut f = File::open("tests/telegraf/telegraf.txt").unwrap();
let mut buff = String::new();
f.read_to_string(&mut buff).unwrap();
let i = parse_telegraf(&buff, None).unwrap();
println!("result: {:#?}", i);
}
fn parse_telegraf(output: &str, point_name: Option<&str>) -> MetricsResult<TsPoint> {
let mut point = TsPoint::new(point_name.unwrap_or("telegraf"), true);
for line in output.lines() {
if line.starts_with('#') {
continue;
}
if line.is_empty() {
continue;
}
let start_index = line.chars().position(|c| c == '{');
let end_index = line.chars().position(|c| c == '}');
let (counter_name, counter_value, tags_text) =
if start_index.is_some() && end_index.is_some() {
let counter_name = &line[0..start_index.unwrap()];
let counter_value = line[end_index.unwrap() + 1..].trim_start();
let tags_text = &line[start_index.unwrap() + 1..end_index.unwrap()];
(counter_name, counter_value, Some(tags_text))
} else {
let parts: Vec<&str> = line.split_whitespace().collect();
(parts[0], parts[1], None)
};
if counter_name.starts_with("go")
|| counter_name.starts_with("disk_inodes")
|| counter_name.starts_with("dm")
|| counter_name.starts_with("disk_used_percent")
|| counter_name.starts_with("net_icmp")
|| counter_name.starts_with("net_udp")
|| counter_name.starts_with("maas_disk")
|| counter_name.starts_with("cpu_usage_guest")
|| counter_name.starts_with("cpu_usage_steal")
{
continue;
}
let mut hmap = HashMap::new();
if let Some(tags_text) = tags_text {
let tags = tags_text.split(',').collect::<Vec<&str>>();
for tag in tags {
let pair = tag.split_terminator('=').collect::<Vec<&str>>();
hmap.insert(pair[0].to_string(), pair[1].to_string());
}
}
if hmap.contains_key("name") {
let n = &hmap["name"].trim_matches('"');
if n.starts_with("loop") || n.starts_with("dm") {
continue;
}
point.add_field(
format!("{}_{}", counter_name.trim_matches('"'), n),
TsValue::Float(f64::from_str(counter_value)?),
);
} else if hmap.contains_key("interface") {
point.add_field(
format!(
"{}_{}",
counter_name.trim_matches('"'),
&hmap["interface"].trim_matches('"')
),
TsValue::Float(f64::from_str(counter_value)?),
);
} else {
point.add_field(counter_name, TsValue::Float(f64::from_str(counter_value)?));
}
}
Ok(point)
}
pub fn get_metrics(
client: &reqwest::Client,
config: &TelegrafConfig,
endpoint: &str,
) -> MetricsResult<TsPoint> {
let url = format!("http://{}:{}/metrics", endpoint, config.port);
let text = client
.get(&url)
.basic_auth(&config.user, Some(&config.password))
.send()?
.error_for_status()?
.text()?;
let points = parse_telegraf(&text, Some("ceph_telegraf"))?;
Ok(points)
}