2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <sys/syscall.h>
31 #include <netinet/in.h>
33 #include "elliptics/packet.h"
34 #include "elliptics/interface.h"
39 #define __unused __attribute__ ((unused))
42 static struct dnet_log stat_logger
;
43 static int stat_mem
, stat_la
, stat_fs
;
45 static int stat_complete(struct dnet_net_state
*state
,
56 if (is_trans_destroyed(state
, cmd
))
59 if (cmd
->size
!= sizeof(struct dnet_stat
))
62 if (!stat_mem
&& !stat_la
&& !stat_fs
)
65 gettimeofday(&tv
, NULL
);
66 localtime_r((time_t *)&tv
.tv_sec
, &tm
);
67 strftime(str
, sizeof(str
), "%F %R:%S", &tm
);
69 fprintf(stream
, "%s.%06lu :", str
, (unsigned long)tv
.tv_usec
);
71 st
= (struct dnet_stat
*)(cmd
+ 1);
73 dnet_convert_stat(st
);
75 la
[0] = (float)st
->la
[0] / 100.0;
76 la
[1] = (float)st
->la
[1] / 100.0;
77 la
[2] = (float)st
->la
[2] / 100.0;
80 fprintf(stream
, "%s: %s: ", dnet_dump_id(&cmd
->id
), dnet_state_dump_addr(state
));
83 fprintf(stream
, "la: %3.2f %3.2f %3.2f ", la
[0], la
[1], la
[2]);
86 fprintf(stream
, "mem: total: %8llu kB, free: %8llu kB, cache: %8llu kB, buffers: %8llu, active: %8llu, inactive: %8llu ",
87 (unsigned long long)st
->vm_total
, (unsigned long long)st
->vm_free
,
88 (unsigned long long)st
->vm_cached
, (unsigned long long)st
->vm_buffers
,
89 (unsigned long long)st
->vm_active
, (unsigned long long)st
->vm_inactive
);
92 fprintf(stream
, "fs: total: %8llu mB, avail: %8llu/%8llu mB ",
93 (unsigned long long)(st
->frsize
* st
->blocks
/ 1024 / 1024),
94 (unsigned long long)(st
->bavail
* st
->bsize
/ 1024 / 1024),
95 (unsigned long long)(st
->bfree
* st
->bsize
/ 1024 / 1024));
97 fprintf(stream
, "\n");
103 static void stat_usage(char *p
)
105 fprintf(stderr
, "Usage: %s\n"
106 " -r addr:port:family - adds a route to the given node\n"
107 " -l log - log file. Default: disabled\n"
108 " -L log - statistics log. Default: stdout\n"
109 " -w timeout - wait timeout in seconds used to wait for content sync.\n"
110 " -m level - log level\n"
111 " -I id - request statistics from node which handles given id\n"
112 " -t timeout - timeout in seconds to repeatedly request statistics\n"
113 " -M - show memory usage statistics\n"
114 " -F - show filesystem usage statistics\n"
115 " -A - show load average statistics\n"
119 int main(int argc
, char *argv
[])
121 int ch
, err
, i
, have_remote
= 0;
122 struct dnet_node
*n
= NULL
;
123 struct dnet_session
*s
= NULL
;
124 struct dnet_config cfg
, rem
;
125 int max_id_idx
= 1000, id_idx
= 0;
127 unsigned char id
[max_id_idx
][DNET_ID_SIZE
];
128 char *logfile
= "/dev/stderr", *statfile
= "/dev/stdout";
129 FILE *log
= NULL
, *stat
;
131 memset(&cfg
, 0, sizeof(struct dnet_config
));
133 cfg
.sock_type
= SOCK_STREAM
;
134 cfg
.proto
= IPPROTO_TCP
;
135 cfg
.wait_timeout
= 60*60;
136 stat_logger
.log_level
= DNET_LOG_ERROR
;
140 memcpy(&rem
, &cfg
, sizeof(struct dnet_config
));
142 while ((ch
= getopt(argc
, argv
, "MFAt:m:w:l:I:r:h")) != -1) {
154 timeout
= atoi(optarg
);
157 stat_logger
.log_level
= strtoul(optarg
, NULL
, 0);
160 cfg
.wait_timeout
= atoi(optarg
);
169 if (id_idx
< max_id_idx
) {
170 err
= dnet_parse_numeric_id(optarg
, id
[id_idx
]);
177 err
= dnet_parse_addr(optarg
, &rem
);
190 fprintf(stderr
, "No remote node specified to route requests.\n");
194 log
= fopen(logfile
, "a");
197 fprintf(stderr
, "Failed to open log file %s: %s.\n", logfile
, strerror(errno
));
201 stat_logger
.log_private
= log
;
202 stat_logger
.log
= dnet_common_log
;
203 cfg
.log
= &stat_logger
;
205 stat
= fopen(statfile
, "a");
208 fprintf(stderr
, "Failed to open stat file %s: %s.\n", statfile
, strerror(errno
));
212 n
= dnet_node_create(&cfg
);
216 s
= dnet_session_create(n
);
220 err
= dnet_add_state(n
, &rem
);
228 err
= dnet_request_stat(s
, NULL
, DNET_CMD_STAT
, 0, stat_complete
, stat
);
233 for (i
=0; i
<id_idx
; ++i
) {
234 dnet_setup_id(&raw
, 0, id
[i
]);
235 err
= dnet_request_stat(s
, &raw
, DNET_CMD_STAT
, 0, stat_complete
, stat
);