-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.c
149 lines (116 loc) · 3.98 KB
/
disk.c
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
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "disk.h"
long int get_disk_size(char *disk_name) {
long int disk_size;
FILE *disk_size_file;
disk_size_file = NULL;
char disk_size_file_path[PATH_MAX];
int ret_snprintf;
ret_snprintf = snprintf(disk_size_file_path, sizeof(disk_size_file_path), "/sys/block/%s/size", disk_name);
if (ret_snprintf < 0) {
goto handle_error;
}
/* read /sys/block/<device>/size file */
disk_size_file = fopen(disk_size_file_path, "r");
if (disk_size_file == NULL) {
goto handle_error;
}
int ret_fscanf = fscanf(disk_size_file, "%ld", &disk_size);
if (ret_fscanf < 1 || ret_fscanf == EOF) {
goto handle_error;
}
/* close file handle */
fclose(disk_size_file);
return disk_size;
handle_error:
if (disk_size_file != NULL) {
fclose(disk_size_file);
}
return 0;
}
int get_disk_sector_size(char *disk_name) {
int disk_sector_size;
FILE *disk_sector_size_file;
disk_sector_size_file = NULL;
char disk_sector_size_file_path[PATH_MAX];
int ret_snprintf;
ret_snprintf = snprintf(disk_sector_size_file_path, sizeof(disk_sector_size_file_path), "/sys/block/%s/queue/logical_block_size", disk_name);
if (ret_snprintf < 0) {
goto handle_error;
}
/* read /sys/block/<device>/queue/logical_block_size file */
disk_sector_size_file = fopen(disk_sector_size_file_path, "r");
if (disk_sector_size_file == NULL) {
goto handle_error;
}
int ret_fscanf = fscanf(disk_sector_size_file, "%d", &disk_sector_size);
if (ret_fscanf < 1 || ret_fscanf == EOF) {
goto handle_error;
}
/* close file handle */
fclose(disk_sector_size_file);
return disk_sector_size;
handle_error:
if (disk_sector_size_file != NULL) {
fclose(disk_sector_size_file);
}
/* return DEFAULT_SECTOR_SIZE if call is failed */
return DEFAULT_SECTOR_SIZE;
}
int get_disk_metrics(struct disk_metrics *input_disk_metrics) {
/* disk count */
int count = 0;
/* placeholder for unused fields */
long int unused;
/* disk metrics variables */
char disk_name[DISK_NAME_LEN];
long int reads;
long int sector_read;
long int writes;
long int sector_write;
FILE *diskstats_file;
diskstats_file = NULL;
/* read line buffer variable */
char line[BUFSIZ];
/* read /proc/diskstats file */
diskstats_file = fopen("/proc/diskstats", "r");
if (diskstats_file == NULL) {
goto handle_error;
}
while (fgets(line, sizeof(line), diskstats_file) != NULL) {
int ret_sscanf = sscanf(line, " %ld %ld %s %ld %ld %ld %ld %ld %ld %ld %*s",
&unused, &unused, disk_name, &reads, &unused, §or_read, &unused, &writes, &unused, §or_write
);
if (ret_sscanf < 5 || ret_sscanf == EOF) {
fprintf(stderr, "ERROR: unable to parse line %s\n", line);
continue;
}
/* we only fill out disk metrics if disk size > 0 */
if (get_disk_size(disk_name) > 0) {
strcpy(input_disk_metrics->diskstats[count].disk_name, disk_name);
input_disk_metrics->diskstats[count].sector_size = get_disk_sector_size(disk_name);
input_disk_metrics->diskstats[count].reads = reads;
input_disk_metrics->diskstats[count].sector_read = sector_read;
input_disk_metrics->diskstats[count].writes = writes;
input_disk_metrics->diskstats[count].sector_write = sector_write;
++count;
} else {
continue;
}
/* stop parsing if number of disks is greater than DISK_COUNT */
if (count >= DISK_COUNT) {
fprintf(stderr, "ERROR: number of disks is greater than %d\n", DISK_COUNT);
break;
}
}
/* close file handle */
fclose(diskstats_file);
return count;
handle_error:
if (diskstats_file != NULL) {
fclose(diskstats_file);
}
return -1;
}