megacli: run megacli via sudo
lgz committed
Jul 5, 2018 at 20:40 UTC
8d033934bd24e3f48984853a485dfb727d6154f8
2 files changed
+16
-8
python.d/megacli.chart.py
+15
-7
@@ -102,14 +102,16 @@ class PD:
102
}
103
104
105
-class Command:
105
+class Megacli:
106
def __init__(self):
107
- self.command = find_binary('megacli')
108
- self.disk_info = [self.command, '-LDPDInfo', '-aAll']
109
- self.battery_info = [self.command, '-AdpBbuCmd', '-a0']
107
+ self.s = find_binary('sudo')
108
+ self.m = find_binary('megacli')
109
+ self.sudo_check = [self.s, '-n', '-v']
110
+ self.disk_info = [self.s, '-n', self.m, '-LDPDInfo', '-aAll']
111
+ self.battery_info = [self.s, '-n', self.m, '-AdpBbuCmd', '-a0']
112
113
def __bool__(self):
112
- return bool(self.command)
114
+ return bool(self.s and self.m)
115
116
def __nonzero__(self):
117
return self.__bool__()
@@ -120,13 +122,19 @@ class Service(ExecutableService):
122
ExecutableService.__init__(self, configuration=configuration, name=name)
123
self.order = list()
124
self.definitions = dict()
123
- self.megacli = Command()
125
+ self.megacli = Megacli()
126
127
def check(self):
128
if not self.megacli:
127
- self.error("can't locate \"megacli\" binary or binary is not executable by netdata")
129
+ self.error('can\'t locate "sudo" or "megacli" binary')
130
return None
131
132
+ err = self._get_raw_data(command=self.megacli.sudo_check, stderr=True)
133
+
134
+ if err:
135
+ self.error(''.join(err))
136
+ return False
137
+
138
d = self._get_raw_data(command=self.megacli.disk_info)
139
140
if not d:
python.d/python_modules/bases/FrameworkServices/ExecutableService.py
+1
-1
@@ -25,7 +25,7 @@ class ExecutableService(SimpleService):
25
try:
26
p = Popen(command if command else self.command, stdout=PIPE, stderr=PIPE)
27
except Exception as error:
28
- self.error('Executing command {command} resulted in error: {error}'.format(command=self.command,
28
+ self.error('Executing command {command} resulted in error: {error}'.format(command=command or self.command,
29
error=error))
30
return None
31
data = list()