aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_hddtemp.c
blob: c8d742bf91ad76787adc9b8c8628bb39a0a6a144 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* $Id$
 * $URL$
 *
 * plugin hddtemp
 *
 * Copyright (C) 2007 Scott Bronson <brons_lcd4linux@rinspin.com>
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* Example:
 * (I tried to put this info on the wiki but Akismet claimed it was spam)

	Widget SmallHDDTemp {
		class 'Text'
		expression hddtemp('/dev/hda')
		width 4
		precision 1
		align 'R'
		update tick
	}

	hddtemp home page: http://www.guzu.net/linux/hddtemp.php

	Quick Examples:
	 * hddtemp() -- return temperature of first drive (order is defined by the hddtemp daemon).
	 * hddtemp('/dev/hda') -- return temperature of /dev/hda on localhost.
	 * hddtemp('ST3400832A') -- return temperature of the drive with the given ID.
	 * hddtemp('burly', '') -- return temperature of the first drive on host burly.
	 * hddtemp('burly', 17634, '/dev/hda') -- return temperature of /dev/hda on burly connecting to the hddtemp daemon listening on port 17634.

You can find out what drives are being monitored by running
		telnet HOST PORT

i.e.

	$ telnet localhost 7634
	Trying 127.0.0.1...
	Connected to localhost.
	Escape character is '^]'.
	|/dev/hda|ST3400832A|53|C|Connection closed by foreign host.

This tells me that /dev/hda is a Seagate ST3400832A running at an awfully
toasty 53 degC.

 */


/* 
 * exported functions:
 *
 * int plugin_init_hddtemp (void)
 *  adds various functions
 * void plugin_exit_hddtemp (void)
 *
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

/* network specific includes */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

/* these should always be included */
#include "debug.h"
#include "plugin.h"
#include "cfg.h"
#include "thread.h"


static int socket_open(const char *name, int port)
{
    struct sockaddr_in server;
    struct hostent *host_info;
    unsigned long addr;
    int sock;

    sock = socket(PF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
	error("[hddtemp] failed to create socket: %s", strerror(errno));
	return -1;
    }

    memset(&server, 0, sizeof(server));
    if ((addr = inet_addr(name)) != INADDR_NONE) {
	memcpy((char *) &server.sin_addr, &addr, sizeof(addr));
    } else {
	host_info = gethostbyname(name);
	if (NULL == host_info) {
	    error("[hddtemp] Unknown server: %s", name);
	    return -1;
	}
	memcpy((char *) &server.sin_addr, host_info->h_addr, host_info->h_length);
    }

    server.sin_family = AF_INET;
    server.sin_port = htons(port);

    if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
	error("[hddtemp] can't connect to server %s: %s", name, strerror(errno));
	return -1;
    }

    return sock;
}


// replaces all null chars in a buffer with a given character.
static void replace_nulls(char *buf, size_t len, char repl)
{
    char *end = buf + len;

    while (buf < end) {
	if (*buf == '\0') {
	    *buf = repl;
	}

	buf += 1;
    }
}


static size_t hddtemp_read(int socket, char *buf, size_t bufsiz)
{
    size_t count = 0;
    ssize_t len;

    while (count < bufsiz) {
	len = read(socket, buf + count, bufsiz - count);
	if (len > 0) {
	    replace_nulls(buf + count, len, '@');
	    count += len;
	} else if (len < 0) {
	    // an error!
	    error("[hddtemp] Couldn't read: %s", strerror(errno));
	    return -1;
	} else {
	    // remote socket has closed
	    break;
	}
    }

    // null-terminate the string
    if (count < bufsiz) {
	buf[count] = '\0';
    } else {
	buf[bufsiz - 1] = '\0';
    }

    return count;
}


static int hddtemp_connect(const char *host, int port, char *buf, size_t bufsiz)
{
    int sock, ret;

    sock = socket_open(host, port);
    if (sock < 0) {
	error("[hddtemp] Error accessing %s:%d: %s", host, port, strerror(errno));
	return -1;
    }

    ret = hddtemp_read(sock, buf, bufsiz);
    close(sock);

    return ret;
}


static char *hddtemp_fetch(char *buf, size_t bufsiz, const char *host, int port, const char *arg)
{
    char *line, *end;

    // fetch a buffer of all hddtemps
    if (!hddtemp_connect(host, port, buf, bufsiz)) {
	return "err";
    }
    // find the line containing the user's specification
    line = strstr(buf, arg);
    if (!line) {
	return "--";
    }
    // back up to the beginning of the line
    while (line > buf && line[-1] != '\n') {
	line--;
    }

    // quick sanity check -- each line begins with the separator
    if (*line != '|') {
	return "fmt1";
    }
    // |device|identifier|temp|units|
    // skip 3 bars to find the start of the temperature
    line = strchr(line + 1, '|');
    if (line && line[0])
	line = strchr(line + 1, '|');
    if (!line || !line[0]) {
	return "fmt2";
    }
    // set the next bar to '\0' to null-terminate temp
    line += 1;
    end = strchr(line, '|');
    if (!end) {
	return "fmt3";
    }
    *end = '\0';

    return line;
}


#ifndef TEST

static void hddtemp_call(RESULT * result, int argc, RESULT * argv[])
{
    char buf[4096];
    const char *search = "";
    const char *host = "localhost";
    int port = 7634;
    char *out;

    switch (argc) {
    case 0:
	break;
    case 1:
	search = R2S(argv[0]);
	break;
    case 2:
	host = R2S(argv[0]);
	search = R2S(argv[1]);
    default:
	if (argv > 3) {
	    error("hddtemp(): too many parameters");
	}
	host = R2S(argv[0]);
	port = (int) R2N(argv[1]);
	search = R2S(argv[2]);
    }

    out = hddtemp_fetch(buf, sizeof(buf), host, port, search);
    SetResult(&result, R_STRING, out);
}


int plugin_init_hddtemp(void)
{
    info("Adding hddtemp functions!");

    AddFunction("hddtemp", -1, hddtemp_call);

    return 0;
}


void plugin_exit_hddtemp(void)
{
}
#endif



#ifdef TEST
// Add this to your makefile to make the test_hddtemp app:
// test_hddtemp: plugin_hddtemp.c
//      gcc -Wall -Werror -g -DTEST plugin_hddtemp.c -o $@

#include <stdarg.h>

void message(const int level, const char *format, ...)
{
    va_list ap;

    va_start(ap, format);
    vprintf(format, ap);
    putchar('\n');
    va_end(ap);
}

int main(int argc, char **argv)
{
    char buf[4096];
    char *out = hddtemp_fetch(buf, sizeof(buf), argv[1] ? argv[1] : "/dev/hda");
    printf("OUTPUT: %s\n", out);
    return 0;
}
#endif