- Add excludepid option, which was missing.
- Add more verbose messages and prepend all verbose messages with [DEBUG]
Javier Fernandez-Sanguino
9 years ago
65 | 65 | return 1 |
66 | 66 | |
67 | 67 | def usage(): |
68 | sys.stderr.write('usage: checkrestart [-vhpa] [-bblacklist] [-iignore]\n') | |
68 | sys.stderr.write('usage: checkrestart [-vhpa] [ -b blacklist_file ] [ -i package_name ] [ -e pid ]\n') | |
69 | 69 | |
70 | 70 | def main(): |
71 | 71 | global lc_all_c_env, file_query_check |
84 | 84 | |
85 | 85 | # Process options |
86 | 86 | try: |
87 | opts, args = getopt.getopt(sys.argv[1:], "hvpab:i:n", ["help", "verbose", "packages", "all", "blacklist", "ignore", "nolsof"]) | |
87 | opts, args = getopt.getopt(sys.argv[1:], "hvpab:i:ne:", ["help", "verbose", "packages", "all", "blacklist", "ignore", "nolsof", "excludepid"]) | |
88 | 88 | except getopt.GetoptError, err: |
89 | 89 | # print help information and exit: |
90 | 90 | print str(err) # will print something like "option -x not recognized" |
142 | 142 | # Check if we have lsof, if not, use an alternative mechanism |
143 | 143 | if not find_cmd('lsof') or not useLsof: |
144 | 144 | if verbose and not find_cmd('lsof'): |
145 | print "Lsof is not available in the system. Using alternative mechanism." | |
145 | print "[DEBUG] Lsof is not available in the system. Using alternative mechanism." | |
146 | 146 | toRestart = procfilescheck(blacklist = blacklist, excludepidlist = excludepidlist) |
147 | 147 | else: |
148 | 148 | toRestart = lsoffilescheck(blacklist = blacklist) |
166 | 166 | #services Verbose information |
167 | 167 | if verbose: |
168 | 168 | for process in toRestart: |
169 | print "Process %s (PID: %d) " % (process.program, process.pid) | |
169 | print "[DEBUG] Process %s (PID: %d) " % (process.program, process.pid) | |
170 | 170 | process.listDeleted() |
171 | 171 | |
172 | 172 | packages = {} |
173 | 173 | diverted = None |
174 | 174 | |
175 | 175 | dpkgQuery = ["dpkg-query", "--search"] + programs.keys() |
176 | if verbose: | |
177 | print "[DEBUG] Running:%s" % dpkgQuery | |
176 | 178 | dpkgProc = subprocess.Popen(dpkgQuery, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
177 | 179 | env = lc_all_c_env) |
178 | if verbose: | |
179 | print "Running:%s" % dpkgQuery | |
180 | 180 | while True: |
181 | 181 | line = dpkgProc.stdout.readline() |
182 | 182 | if not line: |
183 | 183 | break |
184 | 184 | if verbose: |
185 | print "Reading line: %s" % line | |
185 | print "[DEBUG] Reading line from dpkg-query: %s" % line | |
186 | 186 | if line.startswith('local diversion'): |
187 | 187 | continue |
188 | 188 | if not ':' in line: |
206 | 206 | packages.setdefault(packagename,Package(packagename)) |
207 | 207 | try: |
208 | 208 | packages[packagename].processes.extend(programs[program]) |
209 | if verbose: | |
210 | print "[DEBUG] Found package %s for program %s" % (packagename, program) | |
209 | 211 | except KeyError: |
210 | 212 | sys.stderr.write ('checkrestart (program not found): %s: %s\n' % (packagename, program)) |
211 | 213 | sys.stdout.flush() |
216 | 218 | # Remove the ignored packages from the list of packages |
217 | 219 | if ignorelist: |
218 | 220 | for i in ignorelist: |
219 | try: | |
220 | del packages[i] | |
221 | except KeyError: | |
222 | continue | |
221 | if i in packages: | |
222 | if verbose: | |
223 | print "[DEBUG] Removing %s from the package list (ignored)" % (i) | |
224 | try: | |
225 | del packages[i] | |
226 | except KeyError: | |
227 | continue | |
223 | 228 | |
224 | 229 | print "(%d distinct packages)" % len(packages) |
225 | 230 | |
230 | 235 | |
231 | 236 | for package in packages.values(): |
232 | 237 | dpkgQuery = ["dpkg-query", "--listfiles", package.name] |
238 | if verbose: | |
239 | print "[DEBUG] Running:%s" % dpkgQuery | |
233 | 240 | dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
234 | 241 | env = lc_all_c_env) |
235 | 242 | while True: |
402 | 409 | file_in_package = False |
403 | 410 | file_regexp = False |
404 | 411 | if verbose: |
405 | print "Checking if file %s belongs to any package" % f | |
412 | print "[DEBUG] Checking if file %s belongs to any package" % f | |
406 | 413 | # First check if the file exists |
407 | 414 | if not os.path.exists(f): |
408 | 415 | if ( f.startswith('/lib/') or f.startswith('/usr/lib/') ) and re.compile("\.so[\d.]+$"): |
420 | 427 | |
421 | 428 | # If it exists, run dpkg-query |
422 | 429 | dpkgQuery = ["dpkg-query", "--search", f ] |
430 | if verbose: | |
431 | print "[DEBUG] Running:%s" % dpkgQuery | |
423 | 432 | dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
424 | 433 | env = lc_all_c_env, close_fds=True) |
425 | 434 | dpkgProc.wait() |
426 | 435 | if verbose: |
427 | print "Running:%s" % dpkgQuery | |
436 | print "[DEBUG] Running:%s" % dpkgQuery | |
428 | 437 | for line in dpkgProc.stdout.readlines(): |
429 | 438 | line = line.strip() |
430 | 439 | if line.find('no path found matching pattern ' + f) > 0: |
436 | 445 | break |
437 | 446 | |
438 | 447 | if file_in_package and verbose: |
439 | print "YES: File belongs to package %s" % package | |
448 | print "[DEBUG] YES: File belongs to package %s" % package | |
440 | 449 | if not file_in_package and verbose: |
441 | print "NO: File does not belongs to any package" | |
450 | print "[DEBUG] NO: File does not belongs to any package" | |
442 | 451 | return file_in_package |
443 | 452 | |
444 | 453 | # Tells if a file has to be considered a deleted file |