Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Reading multiple option values from a config file

$
0
0
I had an issue earlier where my script wasn't reading the option values from my config files. I have solved that issue but I still can't get it to read multiple option values. I want any file that is uploaded with the option values to create an error if the option values don't match what's in the config files. The end product is to allow a non-coder to be able to add option values that get read to a config file. Here are the two config files I'm using and the script.

[Actions]
actionItems = Add,Update,Delete




[Objects]
objects = Host Group, Service Group, Service



#!usr/bin/python


from subprocess import *
from pynag import Model
import sys
import ConfigParser, os
import csv
import getopt
import time
import datetime
from datetime import date
from time import gmtime, strftime
import logging
sys.path.insert(1, '/opt/pynag')
from sys import argv
script, solution_id, input_file = argv

#creating time stamp and returning as a string to add to solution id log name
def timeIzNow():  
    full = time.strftime(" %Y-%m-%d %H:%M:%S")
 
    return full

#set up logging file
LOG_FILENAME = solution_id  + timeIzNow() 
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s %(process)d',
                    datefmt='%d %b %Y %H:%M:%S', 
                    filename=LOG_FILENAME,
		      filemode='w')   
# defining a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# setting a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# telling the handler to use this format
console.setFormatter(formatter)
# adding the handler to the root logger
logging.getLogger('').addHandler(console)

def knownActions():
    #set up configuration Parser
    actions = ConfigParser.RawConfigParser()
    actions.read('/etc/nagios/ingestion/action.cfg')
    #get the action items
    actions = actions.get('Actions','actionItems')
    return actions

def knownObjects():
     #set up configuration Parser
     config = ConfigParser.RawConfigParser()
     config.read('/etc/nagios/ingestion/objectItems.cfg')
     #get the object types
     objects = config.get('Objects', 'objects')
     return objects



#Get inputs and check value and path to file
try:
    current_file = csv.reader(open(input_file, "rb"), delimiter='\t')
except:  
    logging.error('No such file or directory. Please try again')
else:
    for line in current_file:        
        for row in current_file:       
            if solution_id != row[2]:
                logging.error('Solution ID is invalid. Please check the number and try again')        
            elif knownActions() != row[0]:                    
                logging.error("Undefined action")               
            elif knownObjects() != row[1]:              
                logging.error("Unknown object")             
            else:    
                print row
                    
finally:
     print "all error checks done!" 
 
sys.exit(0) 






















































Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>