1 /*
2  * Copyright (c) 2001-2011 Willem Dijkstra
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *    - Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    - Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 /*
32  * Get current disk statistics from kernel and return them in symon_buf as
33  *
34  * total_rxfer, total_wxfer, total_seeks, total_rbytes, total_wbytes
35  *
36  */
37 
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <inttypes.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "conf.h"
49 #include "xmalloc.h"
50 #include "error.h"
51 #include "symon.h"
52 #include "diskname.h"
53 
54 /* Globals for this module start with io_ */
55 static void *io_buf = NULL;
56 static int io_size = 0;
57 static int io_maxsize = 0;
58 struct io_device_stats
59 {
60     u_int64_t read_issued;
61     u_int64_t read_merged;
62     u_int64_t read_sectors;
63     u_int64_t read_milliseconds;
64     u_int64_t write_issued;
65     u_int64_t write_merged;
66     u_int64_t write_sectors;
67     u_int64_t write_milliseconds;
68     u_int64_t progress_ios;
69     u_int64_t progress_milliseconds;
70     u_int64_t progress_weight;
71 };
72 #ifdef HAS_PROC_DISKSTATS
73 char *io_filename = "/proc/diskstats";
74 #else
75 #ifdef HAS_PROC_PARTITIONS
76 char *io_filename = "/proc/partitions";
77 #endif
78 #endif
79 
80 #if defined(HAS_PROC_DISKSTATS) || defined(HAS_PROC_PARTITIONS)
81 void
init_io(struct stream * st)82 init_io(struct stream *st)
83 {
84     struct disknamectx c;
85     size_t lead = sizeof("/dev/") - 1;
86 
87     if (io_buf == NULL) {
88         io_maxsize = SYMON_MAX_OBJSIZE;
89         io_buf = xmalloc(io_maxsize);
90     }
91 
92     if (st->arg == NULL)
93         fatal("io: need a <device>|<devicename> argument");
94 
95     /* Retrieve io stats to search for devicename */
96     gets_io();
97 
98     initdisknamectx(&c, st->arg, st->parg.io, sizeof(st->parg.io));
99 
100     while (nextdiskname(&c) != NULL) {
101         /* devices are named sdX, not /dev/sdX */
102         if (strncmp(st->parg.io, "/dev/", lead) == 0)
103             memmove(&st->parg.io[0], &st->parg.io[0] + lead, sizeof(st->parg.io) - lead);
104 
105         if (strstr(io_buf, st->parg.io)) {
106             if (strcmp(st->arg, st->parg.io) == 0)
107                 info("started module io(%.200s)", st->parg.io);
108             else
109                 info("started module io(%.200s = %.200s)", st->arg, st->parg.io);
110             return;
111         }
112     }
113 
114     warning("io(%.200s): not mounted", st->arg);
115 }
116 
117 void
gets_io()118 gets_io()
119 {
120     int fd;
121     int len;
122     char *p;
123 
124     if ((fd = open(io_filename, O_RDONLY)) < 0) {
125         warning("cannot access %.200s: %.200s", io_filename, strerror(errno));
126         return;
127     }
128 
129     bzero(io_buf, io_maxsize);
130 
131     len = 0;
132     p = io_buf;
133     io_size = 0;
134     while ((len = read(fd, p, io_maxsize - io_size)) > 0) {
135       p += len;
136       io_size += len;
137     }
138     close(fd);
139 
140     if (io_size == io_maxsize) {
141         /* buffer is too small to hold all interface data */
142         io_maxsize += SYMON_MAX_OBJSIZE;
143         if (io_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
144             fatal("%s:%d: dynamic object limit (%d) exceeded for io data",
145                   __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
146         }
147         io_buf = xrealloc(io_buf, io_maxsize);
148         gets_io();
149         return;
150     }
151 
152     if (io_size == -1) {
153         warning("could not read io statistics from %.200s: %.200s", io_filename, strerror(errno));
154     }
155 }
156 
157 int
get_io(char * symon_buf,int maxlen,struct stream * st)158 get_io(char *symon_buf, int maxlen, struct stream *st)
159 {
160     char *line;
161     struct io_device_stats stats;
162 
163     if (io_size <= 0) {
164         return 0;
165     }
166 
167     if ((line = strstr(io_buf, st->parg.io)) == NULL) {
168         warning("could not find disk %.200s = %.200s", st->arg, st->parg.io);
169         return 0;
170     }
171 
172     line += strlen(st->parg.io);
173     bzero(&stats, sizeof(struct io_device_stats));
174 
175     if (11 > sscanf(line, " %" SCNu64 " %" SCNu64
176                           " %" SCNu64 " %" SCNu64
177                           " %" SCNu64 " %" SCNu64
178                           " %" SCNu64 " %" SCNu64
179                           " %" SCNu64 " %" SCNu64
180                           " %" SCNu64 "\n",
181                     &stats.read_issued, &stats.read_merged,
182                     &stats.read_sectors, &stats.read_milliseconds,
183                     &stats.write_issued, &stats.write_merged,
184                     &stats.write_sectors, &stats.write_milliseconds,
185                     &stats.progress_ios, &stats.progress_milliseconds,
186                     &stats.progress_weight)) {
187 #ifdef HAS_PROC_DISKSTATS
188         if (4 > sscanf(line, " %" SCNu64 " %" SCNu64
189                              " %" SCNu64 " %" SCNu64 "\n",
190                        &stats.read_issued, &stats.read_sectors,
191                        &stats.write_issued, &stats.write_sectors)) {
192             warning("could not parse disk statistics for %.200s", st->arg);
193             return 0;
194         }
195     }
196 #else
197         warning("could not parse disk statistics for %.200s", st->arg);
198         return 0;
199     }
200 #endif
201 
202     return snpack(symon_buf, maxlen, st->arg, MT_IO2,
203                   stats.read_issued,
204                   stats.write_issued,
205                   (u_int64_t) 0,
206                   (u_int64_t)(stats.read_sectors * DEV_BSIZE),
207                   (u_int64_t)(stats.write_sectors * DEV_BSIZE));
208 }
209 #else
210 void
init_io(struct stream * st)211 init_io(struct stream *st)
212 {
213     fatal("io module not available");
214 }
215 void
gets_io()216 gets_io()
217 {
218     fatal("io module not available");
219 }
220 int
get_io(char * symon_buf,int maxlen,struct stream * st)221 get_io(char *symon_buf, int maxlen, struct stream *st)
222 {
223     fatal("io module not available");
224 
225     /* NOT REACHED */
226     return 0;
227 }
228 #endif
229