]> git.sur5r.net Git - i3/i3status/blob - src/print_disk_info.c
Adding optional configuration option to "print_disk_info".
[i3/i3status] / src / print_disk_info.c
1 // vim:ts=8:expandtab
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <mntent.h>
7 #include <stdint.h>
8 #include <sys/stat.h>
9 #include <sys/statvfs.h>
10 #include <sys/types.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
12 #include <sys/param.h>
13 #include <sys/mount.h>
14 #endif
15 #include <yajl/yajl_gen.h>
16 #include <yajl/yajl_version.h>
17
18 #include "i3status.h"
19
20 #define BINARY_BASE UINT64_C(1024)
21 #define DECIMAL_BASE UINT64_C(1000)
22
23 #define MAX_EXPONENT 4
24
25 static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
26 static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
27 static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
28
29 /*
30  * Formats bytes according to the given base and set of symbols.
31  *
32  */
33 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
34         double size = bytes;
35         int exponent = 0;
36         while (size >= base && exponent < MAX_EXPONENT) {
37                 size /= base;
38                 exponent += 1;
39         }
40         return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
41 }
42
43 /*
44  * Prints the given amount of bytes in a human readable manner.
45  *
46  */
47 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
48         if (strcasecmp(prefix_type, "decimal") == 0) {
49                 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
50         } else if (strcasecmp(prefix_type, "custom") == 0) {
51                 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
52         } else {
53                 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
54         }
55 }
56
57 /*
58  * Determines whether remaining bytes are below given threshold.
59  *
60  */
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
62 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
63 #else
64 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
65 #endif
66         if (strcasecmp(threshold_type, "percentage_free") == 0) {
67                 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
68         } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
69                 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
70         } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
71                 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
72         } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
73                 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
74         } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
75                 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
76                 double factor = 1;
77
78                 switch (threshold_type[0]) {
79                 case 'T':
80                 case 't':
81                         factor *= base;
82                 case 'G':
83                 case 'g':
84                         factor *= base;
85                 case 'M':
86                 case 'm':
87                         factor *= base;
88                 case 'K':
89                 case 'k':
90                         factor *= base;
91                         break;
92                 default:
93                         return false;
94                 }
95
96                 if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
97                         return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
98                 } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
99                         return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
100                 }
101         }
102
103         return false;
104 }
105
106 /*
107  * Does a statvfs and prints either free, used or total amounts of bytes in a
108  * human readable manner.
109  *
110  */
111 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
112         const char *walk;
113         char *outwalk = buffer;
114         bool colorful_output = false;
115
116         INSTANCE(path);
117
118 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
119         struct statfs buf;
120
121         if (statfs(path, &buf) == -1)
122                 return;
123 #else
124         struct statvfs buf;
125
126         if (statvfs(path, &buf) == -1)
127                 return;
128
129         if (format_not_mounted != NULL) {
130                 FILE *mntentfile = setmntent("/etc/mtab", "r");
131                 struct mntent *m;
132                 bool found = false;
133
134                 while (NULL != (m = getmntent(mntentfile))) {
135                         if (strcmp(m->mnt_dir, path) == 0) {
136                                 found = true;
137                                 break;
138                         }
139                 }
140                 endmntent(mntentfile);
141
142                 if (!found) {
143                         format = format_not_mounted;
144                 }
145         }
146 #endif
147
148         if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
149                 START_COLOR("color_bad");
150                 colorful_output = true;
151         }
152
153         for (walk = format; *walk != '\0'; walk++) {
154                 if (*walk != '%') {
155                         *(outwalk++) = *walk;
156                         continue;
157                 }
158
159                 if (BEGINS_WITH(walk+1, "free")) {
160                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
161                         walk += strlen("free");
162                 }
163
164                 if (BEGINS_WITH(walk+1, "used")) {
165                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
166                         walk += strlen("used");
167                 }
168
169                 if (BEGINS_WITH(walk+1, "total")) {
170                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
171                         walk += strlen("total");
172                 }
173
174                 if (BEGINS_WITH(walk+1, "avail")) {
175                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
176                         walk += strlen("avail");
177                 }
178
179                 if (BEGINS_WITH(walk+1, "percentage_free")) {
180                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
181                         walk += strlen("percentage_free");
182                 }
183
184                 if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
185                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
186                         walk += strlen("percentage_used_of_avail");
187                 }
188
189                 if (BEGINS_WITH(walk+1, "percentage_used")) {
190                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
191                         walk += strlen("percentage_used");
192                 }
193
194                 if (BEGINS_WITH(walk+1, "percentage_avail")) {
195                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
196                         walk += strlen("percentage_avail");
197                 }
198         }
199
200         if (colorful_output)
201                 END_COLOR;
202
203         *outwalk = '\0';
204         OUTPUT_FULL_TEXT(buffer);
205 }