8
8
try :
9
9
import os
10
10
import sys
11
+ import json
11
12
except ImportError as e :
12
13
raise ImportError (str (e ) + "- required module not found" )
13
14
16
17
except ImportError as e :
17
18
pass
18
19
20
+
19
21
class StorageCommon (object ):
20
22
def __init__ (self , diskdev ):
21
23
"""
@@ -25,121 +27,52 @@ def __init__(self, diskdev):
25
27
Block device path for which we need to get information
26
28
"""
27
29
self .DISKSTATS_FILE = "/proc/diskstats"
28
- self .MOUNTS_FILE = "/proc/mounts"
29
- self .FSSTATS_FILE_PREFIX = "/host/storagemon/fs-stats"
30
30
self .diskdev = os .path .basename (diskdev )
31
- self .fs_reads = 0
32
- self .fs_writes = 0
33
- self ._parse_fsstats_file ()
34
-
35
- def _parse_fsstats_file (self ):
36
- """
37
- Function to parse a file containing the previous latest FS IO Reads/Writes values from the corresponding disk's file and saves it to member variables
38
-
39
- Args: None
40
-
41
- Returns: None
42
-
43
- """
44
- filename = self .FSSTATS_FILE_PREFIX + "-" + self .diskdev
45
-
46
- if not os .path .isfile (filename ):
47
- with open (filename , 'w' ) as f :
48
- f .write ("{} {}" .format (self .fs_reads , self .fs_writes ))
49
- return
50
- else :
51
- try :
52
- with open (filename ) as f :
53
- fsstats = f .readline ().strip ()
54
- self .fs_reads = fsstats .split ()[0 ]
55
- self .fs_writes = fsstats .split ()[1 ]
56
- except Exception as e :
57
- os .remove (filename )
58
- pass
59
-
60
-
61
- def _update_fsstats_file (self , value , attr ):
62
- """
63
- Function to update the latest FS IO Reads/Writes (fs_reads/writes + crresponding value parsed from /proc/diskstats) to the disk's fsstats file
64
-
65
- Args: value, 'R' or 'W' to indicate which field to update in the file
66
-
67
- Returns:
68
- N/A
69
- """
70
- filename = self .FSSTATS_FILE_PREFIX + "-" + self .diskdev
71
- fsstats = ""
72
-
73
- if not os .path .isfile (filename ):
74
- if attr == 'R' :
75
- fsstats = ("{} {}" .format (value , self .fs_writes ))
76
- elif attr == 'W' :
77
- fsstats = ("{} {}" .format (self .fs_reads , value ))
78
- else :
79
- with open (filename ) as f :
80
- fsstats = f .readline ().strip ()
81
-
82
- if attr == 'R' :
83
- fsstats = "{} {}" .format (value , fsstats .split ()[1 ])
84
- elif attr == 'W' :
85
- fsstats = "{} {}" .format (fsstats .split ()[0 ], value )
86
-
87
- with open (filename , 'w' ) as f :
88
- f .write (fsstats )
31
+ self .fsstats_reads = 0
32
+ self .fsstats_writes = 0
89
33
90
34
def get_fs_io_reads (self ):
91
35
"""
92
- Function to get the total number of reads on the disk by parsing the /proc/diskstats file
36
+ Function to get the latest reads on the disk by parsing the /proc/diskstats file
93
37
94
38
Returns:
95
- The total number of disk reads
39
+ The total number of procfs reads
96
40
97
41
Args:
98
42
N/A
99
43
"""
100
44
101
- reads = 0
102
-
103
45
if 'psutil' in sys .modules :
104
- reads = int (psutil .disk_io_counters (perdisk = True , nowrap = True )[self .diskdev ].read_count )
46
+ self . fsstats_reads = int (psutil .disk_io_counters (perdisk = True , nowrap = True )[self .diskdev ].read_count )
105
47
else :
106
48
with open (self .DISKSTATS_FILE ) as f :
107
49
statsfile = f .readlines ()
108
50
for line in statsfile :
109
51
if self .diskdev == line .split ()[2 ]:
110
- reads = int (line .split ()[3 ])
52
+ self . fsstats_reads = int (line .split ()[3 ])
111
53
break
112
54
113
- total_reads = int (self .fs_reads ) + reads
114
- self ._update_fsstats_file (total_reads , 'R' )
115
-
116
- return total_reads
117
-
55
+ return self .fsstats_reads
118
56
119
57
def get_fs_io_writes (self ):
120
58
"""
121
- Function to get the total number of disk writes by parsing the /proc/diskstats file
59
+ Function to get the latest disk writes by parsing the /proc/diskstats file
122
60
123
61
Returns:
124
- The total number of disk writes
62
+ The total number of procfs writes
125
63
126
64
Args:
127
65
N/A
128
66
"""
129
67
130
- writes = 0
131
-
132
68
if 'psutil' in sys .modules :
133
- writes = psutil .disk_io_counters (perdisk = True , nowrap = True )[self .diskdev ].write_count
69
+ self . fsstats_writes = psutil .disk_io_counters (perdisk = True , nowrap = True )[self .diskdev ].write_count
134
70
else :
135
71
with open (self .DISKSTATS_FILE ) as f :
136
72
statsfile = f .readlines ()
137
73
for line in statsfile :
138
74
if self .diskdev == line .split ()[2 ]:
139
- writes = int (line .split ()[7 ])
75
+ self . fsstats_writes = int (line .split ()[7 ])
140
76
break
141
77
142
- total_writes = int (self .fs_writes ) + writes
143
- self ._update_fsstats_file (total_writes , 'W' )
144
-
145
- return total_writes
78
+ return self .fsstats_writes
0 commit comments