Package pyhai :: Package plugins :: Package linux :: Module disk
[hide private]
[frames] | no frames]

Source Code for Module pyhai.plugins.linux.disk

 1  """ 
 2  @copyright: 2011 Mark LaPerriere 
 3   
 4  @license: 
 5      Licensed under the Apache License, Version 2.0 (the "License"); 
 6      you may not use this file except in compliance with the License. 
 7      You may obtain a copy of the License at 
 8   
 9      U{http://www.apache.org/licenses/LICENSE-2.0} 
10   
11      Unless required by applicable law or agreed to in writing, software 
12      distributed under the License is distributed on an "AS IS" BASIS, 
13      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14      See the License for the specific language governing permissions and 
15      limitations under the License. 
16   
17  @summary: 
18      A disk auditing plugin that should work with most Redhat-like distros 
19   
20  @author: Mark LaPerriere 
21  @contact: pyhai@mindmind.com 
22  @organization: Mind Squared Design / www.mindmind.com 
23  @version: 0.1.3 
24  @date: Jan 19, 2012 
25  """ 
26  from pyhai.plugins import AuditorPlugin 
27  from subprocess import Popen, PIPE 
28  import shlex 
29  import logging 
30   
31  # set some default logging behavior 
32  _logger = logging.getLogger(__name__) 
33  _logger.addHandler(logging.NullHandler()) 
34   
35 -class DiskPlugin(AuditorPlugin):
36 - def run(self, *args, **kwargs):
37 38 drive_info = {} 39 40 df_cmd = 'df -PT' 41 df_out = Popen(shlex.split(df_cmd), stdout=PIPE).stdout.read().splitlines() 42 _logger.debug('Output from df command [%s]: %s', df_cmd, df_out) 43 df_headers = df_out[0].split() 44 df_table = df_out[1:] 45 try: 46 block_size = int(df_headers[2].split('-')[0]) 47 except: 48 _logger.exception('Could not coax block size into an integer: %s', headers[1]) 49 return drive_info 50 51 for row in df_table: 52 columns = row.split() 53 drive = columns[6] 54 if drive not in drive_info: 55 drive_info[drive] = {} 56 57 drive_info[drive]['total_bytes'] = long(long(columns[2]) / block_size) 58 drive_info[drive]['available_bytes'] = long(long(columns[4]) / block_size) 59 drive_info[drive]['label'] = columns[0] 60 drive_info[drive]['filesystem'] = columns[1] 61 62 63 return drive_info
64