Python – Parse URLs from files in directory to check if links are up/down before executing yum on Centos.

This script parses the files in the yum repo directory and checks if all URLS are up before proceeding to execute yum. It creates two lists to do a sanity check. One list stores the number of URLs found and the second list stores the number of links that have been found to be working. If those two numbers do not match the execution of YUM is aborted. Please note, this script will work only with custom made yum repos.

import os
import subprocess
import re
import urllib2
import os.path

def check_Software():
    path = '/usr/bin/gcc'
    if os.path.isfile(path):
  ["yum", "install", "-y", "gcc"])
            if not os.patch.isfile(path):
                print "Sending email"

        except OSError as er:
            print er

def yum_link_Search():
    #These two lists are being used as control. The status list is used to append succesful HTTP connections
    #for each of the URLs captured from the files in the YUM directory. The latter is stored in the files_list list.
    #The number (amount of elements) is being compared. A success condition is when each site returns an alive code of
    #200. This tells us that all sites are up and running. View lines between 52 and 60
    status_list = []
    files_list = []
    src_dict = ("/etc/yum.repos.d/") #Specify base directory
    pattern = re.compile ('http\S+') #CPatter to search for

    for yum_files in os.listdir(src_dict): # obtain list of files in directory
        files = os.path.join(src_dict, yum_files) #join the full path with the names of the files.
        strng = open(files) #We need to open the files
        for lines in strng.readlines(): #We then need to read the files
            if, lines): #If we find the pattern we are looking for
                print re.split(r'=', lines)[1] #We split using as a delimeter the = sign.

                    status_link = urllib2.urlopen(re.split(r'=', lines)[1]).getcode()
                    #if  status_link == 200: #Check if the site is up
                    status_list.append(status_link)#View notes starting on line 29 for these conditionals
                    print status_list
                    print files
                    if len(files_list) > len(status_list): #View notes starting on line 29 for these conditionals
                        print "Not all Links are up - ABORTING"
                    if len(files_list) < len(status_list):
                        print "Not all Links are up - ABORTING"
                    elif len(files_list) == len(status_list):
                        print "Will run yum installer" #here we can call other functions since everything seems to be working fine.
                        #call the yum function here
                except StandardError as er:#Here we are capturing errors of other codes.
                    print "Site down"
                    print status_link

Share Button