Added copyright/GPL header to all .c and .h files in trunk
[collectd.git] / src / tape.c
1 /**
2  * collectd - src/tape.c
3  * Copyright (C) 2005  Scott Garrett
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Scott Garrett <sgarrett at technomancer.com>
21  **/
22
23 #include "tape.h"
24
25 #if COLLECT_TAPE
26 #define MODULE_NAME "tape"
27
28 #include "plugin.h"
29 #include "common.h"
30
31 #if defined(HAVE_LIBKSTAT)
32 #define MAX_NUMTAPE 256
33 extern kstat_ctl_t *kc;
34 static kstat_t *ksp[MAX_NUMTAPE];
35 static int numtape = 0;
36 #endif /* HAVE_LIBKSTAT */
37
38 static char *tape_filename_template = "tape-%s.rrd";
39
40 /* 104857600 == 100 MB */
41 static char *tape_ds_def[] =
42 {
43         "DS:rcount:COUNTER:25:0:U",
44         "DS:rmerged:COUNTER:25:0:U",
45         "DS:rbytes:COUNTER:25:0:U",
46         "DS:rtime:COUNTER:25:0:U",
47         "DS:wcount:COUNTER:25:0:U",
48         "DS:wmerged:COUNTER:25:0:U",
49         "DS:wbytes:COUNTER:25:0:U",
50         "DS:wtime:COUNTER:25:0:U",
51         NULL
52 };
53 static int tape_ds_num = 8;
54
55 extern time_t curtime;
56
57 void tape_init (void)
58 {
59 #ifdef HAVE_LIBKSTAT
60         kstat_t *ksp_chain;
61
62         numtape = 0;
63
64         if (kc == NULL)
65                 return;
66
67         for (numtape = 0, ksp_chain = kc->kc_chain;
68                         (numtape < MAX_NUMTAPE) && (ksp_chain != NULL);
69                         ksp_chain = ksp_chain->ks_next)
70         {
71                 if (strncmp (ksp_chain->ks_class, "tape", 4) )
72                         continue;
73                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
74                         continue;
75                 ksp[numtape++] = ksp_chain;
76         }
77 #endif
78
79         return;
80 }
81
82 void tape_write (char *host, char *inst, char *val)
83 {
84         char file[512];
85         int status;
86
87         status = snprintf (file, 512, tape_filename_template, inst);
88         if (status < 1)
89                 return;
90         else if (status >= 512)
91                 return;
92
93         rrd_update_file (host, file, val, tape_ds_def, tape_ds_num);
94 }
95
96
97 #define BUFSIZE 512
98 void tape_submit (char *tape_name,
99                 unsigned long long read_count,
100                 unsigned long long read_merged,
101                 unsigned long long read_bytes,
102                 unsigned long long read_time,
103                 unsigned long long write_count,
104                 unsigned long long write_merged,
105                 unsigned long long write_bytes,
106                 unsigned long long write_time)
107
108 {
109         char buf[BUFSIZE];
110
111         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
112                                 (unsigned int) curtime,
113                                 read_count, read_merged, read_bytes, read_time,
114                                 write_count, write_merged, write_bytes,
115                                 write_time) >= BUFSIZE)
116                 return;
117
118         plugin_submit (MODULE_NAME, tape_name, buf);
119 }
120
121 #undef BUFSIZE
122
123 void tape_read (void)
124 {
125
126 #if defined(HAVE_LIBKSTAT)
127         static kstat_io_t kio;
128         int i;
129
130         if (kc == NULL)
131                 return;
132
133         for (i = 0; i < numtape; i++)
134         {
135                 if (kstat_read (kc, ksp[i], &kio) == -1)
136                         continue;
137
138                 if (strncmp (ksp[i]->ks_class, "tape", 4) == 0)
139                         tape_submit (ksp[i]->ks_name,
140                                         kio.reads,  0LL, kio.nread,    kio.rtime,
141                                         kio.writes, 0LL, kio.nwritten, kio.wtime);
142         }
143 #endif /* defined(HAVE_LIBKSTAT) */
144 }
145
146 void module_register (void)
147 {
148         plugin_register (MODULE_NAME, tape_init, tape_read, tape_write);
149 }
150
151 #undef MODULE_NAME
152 #endif /* COLLECT_TAPE */