| 1 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | __author__ = 'Kenny Freeman' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | __email__ = '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | __license__ = "ISCL" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | __docformat__ = 'reStructuredText' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import re | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import traceback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | import plumd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | import plumd.plugins | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from plumd.calc import Differential | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | from plumd.util import get_file_map | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class DiskStats(plumd.plugins.Reader): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     """Plugin to measure various kernel metrics from /proc.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     defaults = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'poll.interval': 10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'proc_path': '/proc', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'diskstats_dev_re': "dm-\d", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'diskstats_cols': ["r", "r_merge", "r_sector", "r_time", "w", "w_merge", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                            "w_sector", "w_time", "io_inprog", "io_time", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                            "io_weighted_time"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     def __init__(self, log, config): | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         """Plugin to measure various kernel metrics from /proc. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         :param log: A logger | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         :type log: logging.RootLogger | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         :param config: a plumd.config.Conf configuration helper instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         :type config: plumd.config.Conf | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         super(DiskStats, self).__init__(log, config) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         config.defaults(DiskStats.defaults) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         self.calc = Differential() | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         self.proc_file = fname = "{0}/diskstats".format(config.get('proc_path')) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         self.diskstats_dev_re = re.compile(config.get('diskstats_dev_re')) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         self.diskstats_cols = self.config.get('diskstats_cols') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     def poll(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         """Poll for kernel metrics under /proc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         :rtype: ResultSet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return plumd.ResultSet(self.check()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     def check(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         """Return disk io metrics from proc file diskstats. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         :rtype: plumd.Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         # times in ms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         cols = self.diskstats_cols | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         result = plumd.Result("diskstats") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         dat = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         # read and process /proc/stat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             dat = get_file_map(self.proc_file, 2, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         except Exception as e: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             tb = traceback.format_exc() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             self.log.error("proc_diskstats: exception: {0}: {1}".format(e, tb)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ts = time.time() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         for key, val in dat.items(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if self.diskstats_dev_re.match(key): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             for mname in cols: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 mval = int(val.popleft()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 mstr = "{0}.{1}".format(key, mname) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 dval = self.calc.per_second(mstr, mval, ts) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 result.add(plumd.Int(mstr, dval)) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |         return [result] | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |