Permalink
Browse files

made it actually work

  • Loading branch information...
1 parent 996501a commit f9ceb5d1b9a3cc8309801e4f503603069bbf37a6 Aliaksey Kandratsenka committed Feb 26, 2011
Showing with 80 additions and 2 deletions.
  1. +42 −0 example.escript
  2. +38 −2 sigar_port.c
View
@@ -0,0 +1,42 @@
+#!/usr/bin/env escript
+-export([main/1]).
+
+unpack_data(Bin) ->
+ <<Version:32/native-signed,
+ StructSize:32/native,
+ CPULocalMS:64/native,
+ CPUIdleMS:64/native,
+ SwapTotal:64/native,
+ SwapUsed:64/native,
+ SwapPageIn:64/native,
+ SwapPageOut:64/native,
+ MemTotal:64/native,
+ MemUsed:64/native,
+ MemActualUsed:64/native,
+ MemActualFree:64/native>> = Bin,
+ StructSize = erlang:size(Bin),
+ Version = -1,
+ [{cpu_local_ms, CPULocalMS},
+ {cpu_idle_ms, CPUIdleMS},
+ {swap_total, SwapTotal},
+ {swap_used, SwapUsed},
+ {swap_page_in, SwapPageIn},
+ {swap_page_out, SwapPageOut},
+ {mem_total, MemTotal},
+ {mem_used, MemUsed},
+ {mem_actual_used, MemActualUsed},
+ {mem_actual_free, MemActualFree}].
+
+main([]) ->
+ Port = open_port({spawn_executable, "./sigar_port"},
+ [stream, use_stdio, exit_status,
+ binary, eof, {arg0, "no cigar for alk:)"}]),
+
+ port_command(Port, <<0:32/native>>),
+
+ receive
+ {Port, {data, Data}} ->
+ io:format("~p~n", [unpack_data(Data)]);
+ X ->
+ io:format("unexpected: ~p~n", [X])
+ end.
View
@@ -6,14 +6,31 @@
struct system_stats {
uint32_t version;
uint32_t struct_size;
- uint32_t cpu_something;
- uint32_t memory_something;
+
+ uint64_t cpu_total_ms;
+ uint64_t cpu_idle_ms;
+
+ uint64_t swap_total;
+ uint64_t swap_used;
+ uint64_t swap_page_in;
+ uint64_t swap_page_out;
+
+ uint64_t mem_total;
+ uint64_t mem_used;
+ uint64_t mem_actual_used;
+ uint64_t mem_actual_free;
};
int main(void)
{
+ sigar_t *sigar;
+ sigar_mem_t mem;
+ sigar_swap_t swap;
+ sigar_cpu_t cpu;
struct system_stats reply;
+ sigar_open(&sigar);
+
while (!feof(stdin)) {
int req;
int rv = fread(&req, sizeof(req), 1, stdin);
@@ -24,7 +41,26 @@ int main(void)
memset(&reply, 0, sizeof(reply));
reply.version = (uint32_t)-1;
reply.struct_size = sizeof(reply);
+
+ sigar_mem_get(sigar, &mem);
+ sigar_swap_get(sigar, &swap);
+ sigar_cpu_get(sigar, &cpu);
+
+ reply.cpu_total_ms = cpu.total;
+ reply.cpu_idle_ms = cpu.idle;
+
+ reply.swap_total = swap.total;
+ reply.swap_used = swap.used;
+ reply.swap_page_in = swap.page_in;
+ reply.swap_page_out = swap.page_out;
+
+ reply.mem_total = mem.total;
+ reply.mem_used = mem.used;
+ reply.mem_actual_used = mem.actual_used;
+ reply.mem_actual_free = mem.actual_free;
+
fwrite(&reply, sizeof(reply), 1, stdout);
+ fflush(stdout);
}
return 0;

0 comments on commit f9ceb5d

Please sign in to comment.