Compare commits
No commits in common. "dab10464fee9c61f9688521400825093901c45e4" and "6ed252c5d7c0cc970748d92769e462a6e6f08a2b" have entirely different histories.
dab10464fe
...
6ed252c5d7
1 changed files with 33 additions and 93 deletions
126
src/main.zig
126
src/main.zig
|
@ -67,11 +67,9 @@ fn readConfig(allocator: std.mem.Allocator, path: []const u8) !ConfigWrapper(Con
|
||||||
.arena = try allocator.create(std.heap.ArenaAllocator),
|
.arena = try allocator.create(std.heap.ArenaAllocator),
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
};
|
};
|
||||||
errdefer allocator.destroy(config.arena);
|
config.arena.child_allocator = allocator;
|
||||||
config.arena.* = std.heap.ArenaAllocator.init(allocator);
|
const data = try std.fs.cwd().readFileAlloc(config.arena.child_allocator, path, 1024);
|
||||||
errdefer config.arena.deinit();
|
config.value = try std.json.parseFromSliceLeaky(Config, config.arena.child_allocator, data, .{});
|
||||||
const data = try std.fs.cwd().readFileAlloc(config.arena.allocator(), path, 1024);
|
|
||||||
config.value = try std.json.parseFromSliceLeaky(Config, config.arena.allocator(), data, .{});
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,10 +102,7 @@ pub fn main() !void {
|
||||||
var password_buffer: [128]u8 = undefined;
|
var password_buffer: [128]u8 = undefined;
|
||||||
var password: []const u8 = undefined;
|
var password: []const u8 = undefined;
|
||||||
if (config.value.loki.password_file) |password_file_path| {
|
if (config.value.loki.password_file) |password_file_path| {
|
||||||
password = std.fs.cwd().readFile(password_file_path, &password_buffer) catch |err| {
|
password = try std.fs.cwd().readFile(password_file_path, &password_buffer);
|
||||||
iperf3_log.err("error trying to read password file {s}: {}", .{ password_file_path, err });
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
password = std.mem.trimRight(u8, password, "\r\n");
|
password = std.mem.trimRight(u8, password, "\r\n");
|
||||||
} else if (config.value.loki.password) |password_data| {
|
} else if (config.value.loki.password) |password_data| {
|
||||||
@memcpy(&password_buffer, password_data);
|
@memcpy(&password_buffer, password_data);
|
||||||
|
@ -138,6 +133,9 @@ pub fn main() !void {
|
||||||
|
|
||||||
const uri = try std.Uri.parse(url);
|
const uri = try std.Uri.parse(url);
|
||||||
|
|
||||||
|
var client = std.http.Client{ .allocator = allocator };
|
||||||
|
defer client.deinit();
|
||||||
|
|
||||||
var port_buf: [16]u8 = undefined;
|
var port_buf: [16]u8 = undefined;
|
||||||
var port: []u8 = undefined;
|
var port: []u8 = undefined;
|
||||||
if (config.value.iperf3.port) |p| {
|
if (config.value.iperf3.port) |p| {
|
||||||
|
@ -157,10 +155,7 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
iperf3_log.info("starting new iperf3", .{});
|
iperf3_log.info("waiting for connection", .{});
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
var c = std.process.Child.init(
|
var c = std.process.Child.init(
|
||||||
&[_][]const u8{
|
&[_][]const u8{
|
||||||
|
@ -171,61 +166,33 @@ pub fn main() !void {
|
||||||
"--json",
|
"--json",
|
||||||
"--one-off",
|
"--one-off",
|
||||||
},
|
},
|
||||||
arena.allocator(),
|
allocator,
|
||||||
);
|
);
|
||||||
c.stdin_behavior = .Ignore;
|
c.stdin_behavior = .Ignore;
|
||||||
c.stdout_behavior = .Pipe;
|
c.stdout_behavior = .Pipe;
|
||||||
c.stderr_behavior = .Pipe;
|
c.stderr_behavior = .Ignore;
|
||||||
try c.spawn();
|
try c.spawn();
|
||||||
|
|
||||||
var stdout = std.ArrayList(u8).init(arena.allocator());
|
var reader = c.stdout.?.reader();
|
||||||
var stderr = std.ArrayList(u8).init(arena.allocator());
|
var token_reader = std.json.reader(allocator, reader);
|
||||||
|
|
||||||
iperf3_log.info("waiting for data", .{});
|
var obj = try std.json.parseFromTokenSource(
|
||||||
|
|
||||||
try c.collectOutput(&stdout, &stderr, 16384);
|
|
||||||
|
|
||||||
var filename_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
|
||||||
var filename: []u8 = undefined;
|
|
||||||
var timestamp = std.time.nanoTimestamp();
|
|
||||||
|
|
||||||
filename = try std.fmt.bufPrint(&filename_buf, "/tmp/{d}-stdout.json", .{timestamp});
|
|
||||||
try std.fs.cwd().writeFile(filename, stdout.items);
|
|
||||||
|
|
||||||
filename = try std.fmt.bufPrint(&filename_buf, "/tmp/{d}-stderr.json", .{timestamp});
|
|
||||||
try std.fs.cwd().writeFile(filename, stderr.items);
|
|
||||||
|
|
||||||
// var token_reader = std.json.reader(allocator, reader);
|
|
||||||
|
|
||||||
var obj_or_err = std.json.parseFromSliceLeaky(
|
|
||||||
iperf3.IPerfReturn,
|
iperf3.IPerfReturn,
|
||||||
arena.allocator(),
|
allocator,
|
||||||
stdout.items,
|
&token_reader,
|
||||||
.{},
|
.{},
|
||||||
);
|
);
|
||||||
|
defer obj.deinit();
|
||||||
|
|
||||||
var level = loki.LogLevel.info;
|
var line = std.ArrayList(u8).init(allocator);
|
||||||
if (obj_or_err) |obj| {
|
try std.json.stringify(
|
||||||
iperf3_log.info("successfully parsed json {d}", .{timestamp});
|
obj.value,
|
||||||
if (obj.@"error") |err| {
|
.{
|
||||||
iperf3_log.err("error from iperf3: {s}", .{err});
|
.emit_null_optional_fields = false,
|
||||||
level = loki.LogLevel.@"error";
|
},
|
||||||
}
|
line.writer(),
|
||||||
} else |err| switch (err) {
|
);
|
||||||
error.UnknownField => iperf3_log.err("unknown field parsing json {d}", .{timestamp}),
|
defer line.deinit();
|
||||||
error.UnexpectedEndOfInput => iperf3_log.err("unexpected end of input parsing json {d}", .{timestamp}),
|
|
||||||
else => iperf3_log.err("other error while parsing json {} {d}", .{ err, timestamp }),
|
|
||||||
}
|
|
||||||
|
|
||||||
// var line = std.ArrayList(u8).init(allocator);
|
|
||||||
// try std.json.stringify(
|
|
||||||
// obj.value,
|
|
||||||
// .{
|
|
||||||
// .emit_null_optional_fields = false,
|
|
||||||
// },
|
|
||||||
// line.writer(),
|
|
||||||
// );
|
|
||||||
// defer line.deinit();
|
|
||||||
|
|
||||||
const streams = loki.LokiStreams{
|
const streams = loki.LokiStreams{
|
||||||
.streams = &[_]loki.LokiStream{
|
.streams = &[_]loki.LokiStream{
|
||||||
|
@ -233,19 +200,19 @@ pub fn main() !void {
|
||||||
.stream = .{
|
.stream = .{
|
||||||
.job = "iperf3",
|
.job = "iperf3",
|
||||||
.server = hostname,
|
.server = hostname,
|
||||||
.level = level,
|
.level = if (obj.value.@"error" == null) .info else .@"error",
|
||||||
},
|
},
|
||||||
.values = &[_]loki.LokiValue{
|
.values = &[_]loki.LokiValue{
|
||||||
.{
|
.{
|
||||||
.ts = timestamp,
|
.ts = std.time.nanoTimestamp(),
|
||||||
.line = stdout.items,
|
.line = line.items,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
var data = std.ArrayList(u8).init(arena.allocator());
|
var data = std.ArrayList(u8).init(allocator);
|
||||||
try std.json.stringify(
|
try std.json.stringify(
|
||||||
streams,
|
streams,
|
||||||
.{
|
.{
|
||||||
|
@ -262,49 +229,22 @@ pub fn main() !void {
|
||||||
.{data.items.len},
|
.{data.items.len},
|
||||||
);
|
);
|
||||||
|
|
||||||
var client = std.http.Client{ .allocator = arena.allocator() };
|
var headers = std.http.Headers{ .allocator = allocator };
|
||||||
defer client.deinit();
|
|
||||||
|
|
||||||
var headers = std.http.Headers{ .allocator = arena.allocator() };
|
|
||||||
try headers.append("Authorization", auth_header);
|
try headers.append("Authorization", auth_header);
|
||||||
try headers.append("Accept", "application/json");
|
try headers.append("Accept", "application/json");
|
||||||
try headers.append("Content-Type", "application/json");
|
try headers.append("Content-Type", "application/json");
|
||||||
try headers.append("Content-Length", content_length);
|
try headers.append("Content-Length", content_length);
|
||||||
defer headers.deinit();
|
|
||||||
|
|
||||||
var req = try client.request(
|
var req = try client.request(.POST, uri, headers, .{});
|
||||||
.POST,
|
|
||||||
uri,
|
|
||||||
headers,
|
|
||||||
.{},
|
|
||||||
);
|
|
||||||
defer req.deinit();
|
defer req.deinit();
|
||||||
|
|
||||||
iperf3_log.info("sending stream to loki", .{});
|
|
||||||
|
|
||||||
try req.start();
|
try req.start();
|
||||||
try req.writeAll(data.items);
|
try req.writeAll(data.items);
|
||||||
try req.finish();
|
try req.finish();
|
||||||
try req.wait();
|
try req.wait();
|
||||||
|
|
||||||
if (req.response.status == .no_content) {
|
iperf3_log.info("{}\n", .{req.response.status});
|
||||||
iperf3_log.info("successfully sent stream to loki", .{});
|
|
||||||
} else {
|
|
||||||
iperf3_log.info("response from loki: {}\n", .{req.response.status});
|
|
||||||
}
|
|
||||||
const term = try c.wait();
|
|
||||||
|
|
||||||
switch (term) {
|
_ = try c.wait();
|
||||||
.Exited => |value| {
|
|
||||||
if (value == 0) {
|
|
||||||
iperf3_log.info("iperf3 competed successfully", .{});
|
|
||||||
} else {
|
|
||||||
iperf3_log.info("iperf3 exited with error {d}", .{value});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
iperf3_log.info("iperf3 terminated: {}", .{term});
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue