author | Mikael Berthe <mikael@lilotux.net> |
Thu, 11 Nov 2010 22:21:13 +0100 | |
changeset 5 | d8550798bb1d |
parent 4 | 6ec78c5af989 |
child 6 | 55944382d88b |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
/* Copyright 2010 Myhailo Danylenko |
|
3 |
* |
|
4 |
* This file is part of mcabber-uptime |
|
5 |
* |
|
6 |
* mcabber-uptime is free software: you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation, either version 2 of the License, or |
|
9 |
* (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
18 |
||
19 |
#include <glib.h> |
|
20 |
#include <gmodule.h> |
|
21 |
#include <time.h> |
|
22 |
#include <stdio.h> |
|
1
2b0115cae8b2
Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
23 |
#include <string.h> |
0 | 24 |
|
25 |
#include <mcabber/modules.h> |
|
26 |
#include <mcabber/logprint.h> |
|
27 |
#include <mcabber/commands.h> |
|
28 |
#include <mcabber/settings.h> |
|
29 |
||
30 |
#include "config.h" |
|
31 |
||
32 |
#define DESCRIPTION ( "Shows mcabber uptime\nRecognizes option uptime_hz (at load time)." ) |
|
33 |
||
34 |
module_info_t info_uptime_experimental = { |
|
35 |
.branch = "experimental", |
|
4 | 36 |
.api = 15, |
0 | 37 |
.version = PROJECT_VERSION, |
38 |
.description = DESCRIPTION, |
|
39 |
.requires = NULL, |
|
40 |
.init = NULL, |
|
41 |
.uninit = NULL, |
|
42 |
.next = NULL, |
|
43 |
}; |
|
44 |
||
45 |
module_info_t info_uptime = { |
|
46 |
.branch = "dev", |
|
4 | 47 |
.api = 13, |
0 | 48 |
.version = PROJECT_VERSION, |
49 |
.description = DESCRIPTION, |
|
50 |
.requires = NULL, |
|
51 |
.init = NULL, |
|
52 |
.uninit = NULL, |
|
53 |
.next = &info_uptime_experimental, |
|
54 |
}; |
|
55 |
||
56 |
time_t starttime = 0; |
|
57 |
||
58 |
void do_uptime (char *arg) |
|
59 |
{ |
|
60 |
GString *line = g_string_new (NULL); |
|
61 |
guint seconds = time (NULL) - starttime; |
|
62 |
guint minutes = seconds / 60; |
|
63 |
guint hours = minutes / 60; |
|
64 |
guint days = hours / 24; |
|
65 |
seconds %= 60; |
|
66 |
minutes %= 60; |
|
67 |
hours %= 24; |
|
68 |
||
69 |
if (days) |
|
70 |
g_string_append_printf (line, " %u days", days); |
|
71 |
if (hours) |
|
72 |
g_string_append_printf (line, " %u hours", hours); |
|
73 |
if (minutes) |
|
74 |
g_string_append_printf (line, " %u minutes", minutes); |
|
75 |
if (seconds) |
|
76 |
g_string_append_printf (line, " %u seconds", seconds); |
|
77 |
||
78 |
scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds"); |
|
79 |
||
80 |
g_string_free (line, TRUE); |
|
81 |
} |
|
82 |
||
83 |
gchar *g_module_check_init (GModule *module) |
|
84 |
{ |
|
85 |
if (settings_opt_get_int ("uptime_use_proc")) { |
|
86 |
long long unsigned int mstime = 0; |
|
87 |
long long unsigned int kbtime = 0; |
|
88 |
GError *error = NULL; |
|
89 |
GString *line; |
|
90 |
GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error); |
|
91 |
||
92 |
if (!channel) |
|
93 |
return "Cannot open own stats for reading"; |
|
94 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
95 |
||
96 |
line = g_string_new (NULL); |
|
97 |
if (g_io_channel_read_line_string (channel, line, NULL, &error) != G_IO_STATUS_NORMAL) { |
|
98 |
if (error) { |
|
99 |
scr_log_print (LPRINT_DEBUG, "uptime: Own stats reading error: %s.", error -> message); |
|
100 |
g_error_free (error); |
|
101 |
} |
|
102 |
g_io_channel_unref (channel); |
|
103 |
g_string_free (line, TRUE); |
|
104 |
return "Error reading own stats"; |
|
105 |
} |
|
106 |
g_io_channel_unref (channel); |
|
107 |
gchar *p = strrchr (line -> str, ')'); // end of command |
|
108 |
if (!p) { |
|
109 |
g_string_free (line, TRUE); |
|
1
2b0115cae8b2
Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
110 |
return "Missing ) in own stats"; |
0 | 111 |
} |
112 |
while (*p && isspace (*p)) p++; |
|
113 |
while (*p && isalpha (*p)) p++; // state (%c) |
|
114 |
while (*p && isspace (*p)) p++; |
|
115 |
while (*p && isdigit (*p)) p++; // ppid (this and next are %d) |
|
116 |
while (*p && isspace (*p)) p++; |
|
117 |
while (*p && isdigit (*p)) p++; // pgrp |
|
118 |
while (*p && isspace (*p)) p++; |
|
119 |
while (*p && isdigit (*p)) p++; // session |
|
120 |
while (*p && isspace (*p)) p++; |
|
121 |
while (*p && isdigit (*p)) p++; // tty_nr |
|
122 |
while (*p && isspace (*p)) p++; |
|
123 |
while (*p && isdigit (*p)) p++; // tpgid |
|
124 |
while (*p && isspace (*p)) p++; |
|
125 |
while (*p && isdigit (*p)) p++; // flags (this and all next - %u) |
|
126 |
while (*p && isspace (*p)) p++; |
|
127 |
while (*p && isdigit (*p)) p++; // minflt |
|
128 |
while (*p && isspace (*p)) p++; |
|
129 |
while (*p && isdigit (*p)) p++; // cminflt |
|
130 |
while (*p && isspace (*p)) p++; |
|
131 |
while (*p && isdigit (*p)) p++; // majflt |
|
132 |
while (*p && isspace (*p)) p++; |
|
133 |
while (*p && isdigit (*p)) p++; // cmajflt |
|
134 |
while (*p && isspace (*p)) p++; |
|
135 |
while (*p && isdigit (*p)) p++; // utime |
|
136 |
while (*p && isspace (*p)) p++; |
|
137 |
while (*p && isdigit (*p)) p++; // stime |
|
138 |
while (*p && isspace (*p)) p++; |
|
139 |
while (*p && isdigit (*p)) p++; // cutime (this and next next are %d) |
|
140 |
while (*p && isspace (*p)) p++; |
|
141 |
while (*p && isdigit (*p)) p++; // cstime |
|
142 |
while (*p && isspace (*p)) p++; |
|
143 |
while (*p && isdigit (*p)) p++; // priority |
|
144 |
while (*p && isspace (*p)) p++; |
|
145 |
while (*p && isdigit (*p)) p++; // nice |
|
146 |
while (*p && isspace (*p)) p++; |
|
147 |
while (*p && isdigit (*p)) p++; // num_threads |
|
148 |
while (*p && isspace (*p)) p++; |
|
149 |
while (*p && isdigit (*p)) p++; // itrealvalue |
|
150 |
while (*p && isspace (*p)) p++; |
|
151 |
char *u = p; |
|
152 |
while (*u && isdigit (*u)) u++; |
|
153 |
*u = '\0'; |
|
154 |
if (!sscanf (p, "%llu", &mstime)) { |
|
155 |
scr_log_print (LPRINT_LOGNORM, "uptime: now at %u/%u, remaining string: %s.", p - line -> str, line -> len, p); |
|
156 |
g_string_free (line, TRUE); |
|
157 |
return "Malformed own start time."; |
|
158 |
} |
|
159 |
||
160 |
channel = g_io_channel_new_file ("/proc/stat", "r", &error); |
|
161 |
if (!channel) { |
|
162 |
g_string_free (line, TRUE); |
|
163 |
return "Cannot open system stats for reading"; |
|
164 |
} |
|
165 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
166 |
||
167 |
while (TRUE) { |
|
168 |
GIOStatus ret = g_io_channel_read_line_string (channel, line, NULL, &error); |
|
169 |
if (ret != G_IO_STATUS_NORMAL) { |
|
170 |
if (error) { |
|
171 |
scr_log_print (LPRINT_DEBUG, "uptime: System stats reading error: %s.", error -> message); |
|
172 |
g_error_free (error); |
|
173 |
} |
|
174 |
g_io_channel_unref (channel); |
|
175 |
g_string_free (line, TRUE); |
|
176 |
return "Error reading system stats."; |
|
177 |
} |
|
178 |
if (sscanf (line -> str, "btime %llu", &kbtime)) |
|
179 |
break; |
|
180 |
} |
|
181 |
g_io_channel_unref (channel); |
|
182 |
||
183 |
g_string_free (line, TRUE); |
|
184 |
||
185 |
guint hz = settings_opt_get_int ("uptime_hz"); |
|
186 |
if (!hz) |
|
1
2b0115cae8b2
Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
187 |
hz = 100; |
0 | 188 |
starttime = kbtime + (mstime / hz); |
1
2b0115cae8b2
Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
189 |
} else |
0 | 190 |
starttime = time (NULL); |
191 |
||
192 |
cmd_add ("uptime", "", 0, 0, do_uptime, NULL); |
|
193 |
return NULL; |
|
194 |
} |
|
195 |
||
196 |
void g_module_unload (GModule *module) |
|
197 |
{ |
|
198 |
cmd_del ("uptime"); |
|
199 |
} |
|
200 |
||
201 |
/* The End */ |