|
| 1 | +### Author: Scott Baker - <https://www.perturb.org/> |
| 2 | + |
| 3 | +class dool_plugin(dool): |
| 4 | + """ |
| 5 | + Plugin to gather data about a specific PID |
| 6 | + """ |
| 7 | + |
| 8 | + def __init__(self): |
| 9 | + self.vars = ('cpu read writ mem',) |
| 10 | + self.type = 's' |
| 11 | + self.width = 20 |
| 12 | + self.scale = 0 |
| 13 | + self.prev = { 'read': -1, 'write': -1, 'cpu': 0, 'mem': 0} |
| 14 | + |
| 15 | + # Get the pid from the CLI options |
| 16 | + try: |
| 17 | + pid = int(op.plugin_params['pid-detail']) |
| 18 | + except: |
| 19 | + msg = 'pid-detail: %s is not a number' % (op.plugin_params['pid-detail']) |
| 20 | + raise Exception(msg) |
| 21 | + |
| 22 | + path = '/proc/%s/stat' % pid |
| 23 | + if not os.path.exists(path): |
| 24 | + msg = 'pid-detail: %d is not an active pid' % (pid) |
| 25 | + raise Exception(msg) |
| 26 | + |
| 27 | + name = getnamebypid(pid, 'unknown') |
| 28 | + |
| 29 | + # Set the plugin title at the top of the column |
| 30 | + self.name = "pid info: %s" % name |
| 31 | + |
| 32 | + # Save the pid name and number for easier access later |
| 33 | + self.pid_name = name |
| 34 | + self.pid = pid |
| 35 | + |
| 36 | + def extract(self): |
| 37 | + pid = self.pid |
| 38 | + |
| 39 | + # Make sure we have data for this pid |
| 40 | + path = '/proc/%s/stat' % pid |
| 41 | + if not os.path.exists(path): |
| 42 | + self.output = 'Pid #%s not found' % pid |
| 43 | + return |
| 44 | + |
| 45 | + (disk_rd, disk_wr) = self.get_pid_disk(pid) |
| 46 | + cpu_usage = self.get_pid_cpu(pid) |
| 47 | + mem_usage = self.get_pid_mem(pid) |
| 48 | + |
| 49 | + cpu_str = cprint(cpu_usage, 'f', 3, 34) |
| 50 | + disk_rd = cprint(disk_rd , 'd', 5, 1024) |
| 51 | + disk_wr = cprint(disk_wr , 'd', 5, 1024) |
| 52 | + mem_usage = cprint(mem_usage, 'd', 4, 1024) |
| 53 | + |
| 54 | + self.output = "%s %s %s %s" % (cpu_str, disk_rd, disk_wr, mem_usage) |
| 55 | + |
| 56 | + # Sometimes /proc/12345/stat has a space in the proc name field |
| 57 | + # which messes up the split. This tries to be smart and work around that |
| 58 | + def get_proc_pid_stat(self, pid): |
| 59 | + path = '/proc/%s/stat' % pid |
| 60 | + if not os.path.exists(path): |
| 61 | + return [] |
| 62 | + |
| 63 | + # Read the first line |
| 64 | + line = proc_readline(path) |
| 65 | + line = line.strip() |
| 66 | + |
| 67 | + # Find the proc name that's between the ( ) |
| 68 | + x = re.search("\((.*?)\)", line) |
| 69 | + paren = x.group(1) |
| 70 | + |
| 71 | + # Replace any spaces in the parens part with underscores |
| 72 | + cleaned = paren.replace(" ", "_") |
| 73 | + new_line = line.replace(paren, cleaned, 1) |
| 74 | + |
| 75 | + # Return the split array |
| 76 | + parts = new_line.split(" ") |
| 77 | + |
| 78 | + return parts |
| 79 | + |
| 80 | + def get_pid_cpu(self, pid): |
| 81 | + path = '/proc/%s/stat' % pid |
| 82 | + if not os.path.exists(path): |
| 83 | + self.output = 'Pid #%s not found' % pid |
| 84 | + |
| 85 | + l = self.get_proc_pid_stat(pid) |
| 86 | + if len(l) < 35: return 0 |
| 87 | + |
| 88 | + user_time = int(l[13]) |
| 89 | + kernel_time = int(l[14]) |
| 90 | + |
| 91 | + current_cpu = user_time + kernel_time |
| 92 | + prev_cpu = self.prev['cpu'] |
| 93 | + usage = (current_cpu - prev_cpu) * 1.0 / elapsed / cpunr |
| 94 | + |
| 95 | + if prev_cpu: |
| 96 | + ret = usage |
| 97 | + else: |
| 98 | + ret = 0 |
| 99 | + |
| 100 | + # If we're at a real update (not interim) we save the data |
| 101 | + if step == op.delay: |
| 102 | + self.prev['cpu'] = current_cpu |
| 103 | + |
| 104 | + return ret |
| 105 | + |
| 106 | + def get_pid_disk(self, pid): |
| 107 | + path = '/proc/%s/io' % pid |
| 108 | + count = 0 |
| 109 | + for p in proc_splitlines(path): |
| 110 | + key = p[0] |
| 111 | + val = p[1] |
| 112 | + |
| 113 | + if key == "read_bytes:": |
| 114 | + cur_read = int(val) |
| 115 | + elif key == "write_bytes:": |
| 116 | + cur_write = int(val) |
| 117 | + |
| 118 | + count += 1 |
| 119 | + |
| 120 | + # This is usually a permission error reading that part of /proc/ |
| 121 | + if count == 0: |
| 122 | + return (0,0) |
| 123 | + |
| 124 | + if (op.bits): |
| 125 | + factor = 8 |
| 126 | + else: |
| 127 | + factor = 1 |
| 128 | + |
| 129 | + # We have previous data so we calculate the difference |
| 130 | + # between the two |
| 131 | + if self.prev['read'] > -1: |
| 132 | + rread = (cur_read - self.prev['read']) * factor / elapsed |
| 133 | + rwrite = (cur_write - self.prev['write']) * factor / elapsed |
| 134 | + # First round we just gather data |
| 135 | + else: |
| 136 | + rread = 0 |
| 137 | + rwrite = 0 |
| 138 | + |
| 139 | + # If we're at a real update (not interim) we save the data |
| 140 | + if step == op.delay: |
| 141 | + self.prev['read'] = cur_read |
| 142 | + self.prev['write'] = cur_write |
| 143 | + |
| 144 | + ret = (rread, rwrite) |
| 145 | + |
| 146 | + return ret |
| 147 | + |
| 148 | + def get_pid_mem(self, pid): |
| 149 | + l = proc_splitline('/proc/%s/statm' % pid) |
| 150 | + |
| 151 | + if len(l) < 2: return 0 |
| 152 | + |
| 153 | + prev_usage = self.prev['mem'] |
| 154 | + cur_usage = (int(l[1]) * pagesize) |
| 155 | + |
| 156 | + # If we have previous numbers we return the average |
| 157 | + if prev_usage: |
| 158 | + usage = self.prev['mem'] / elapsed |
| 159 | + # First run just gets the current number |
| 160 | + else: |
| 161 | + usage = cur_usage |
| 162 | + |
| 163 | + self.prev['mem'] += cur_usage |
| 164 | + |
| 165 | + if step == op.delay: |
| 166 | + self.prev['mem'] = cur_usage |
| 167 | + |
| 168 | + # Uncomment this to always return the absolute (not average) |
| 169 | + #usage = cur_usage |
| 170 | + |
| 171 | + return usage |
| 172 | + |
| 173 | + def showcsv(self): |
| 174 | + xstr = "%s %s %s %s" % (1,2,3,4) |
| 175 | + |
| 176 | + return xstr |
| 177 | + |
| 178 | +# vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4 |
0 commit comments