Пример #1
0
def setPoll(args, fromCLI):
    paramsReq = (ARG_URI, )
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)

    ensureClientEnabled(fromCLI)
    return editClient({ARG_DEPSERVURI: args[ARG_URI]}, fromCLI)
Пример #2
0
def i18n_extract(args, fromCLI):
    splunk_home = comm.splunk_home
    params_req = ('app',)
    params_opt = ()
    comm.validateArgs(params_req, params_opt, args)

    app_path = os.path.join(splunk_home, 'etc', 'apps', args['app'])
    app_locale_path = os.path.join(app_path, 'locale')
    if not os.path.exists(app_locale_path):
        os.makedirs(app_locale_path)

    messages_pot = os.path.join(app_locale_path, 'messages.pot')

    babel_cfg = os.path.join(app_locale_path, 'babel.cfg')
    if not os.path.exists(babel_cfg):
        from splunk.appserver.mrsparkle.lib import i18n
        mrsparkle = os.path.dirname(os.path.dirname(i18n.__file__))
        babel_cfg = os.path.join(mrsparkle, 'locale', 'babel.cfg')
    
    args = [ 
        'extract', 
        '-F',  babel_cfg,
        '-o', messages_pot,
        '-c', 'TRANS:',
        app_path
        ]

    sys.argv[1:] = args

    frontend.main()
Пример #3
0
def imUserSplunk(args, fromCLI):
  """
  Import users and splunks.
  """
  paramsReq = ("dir",)
  paramsOpt = ()

  comm.validateArgs(paramsReq, paramsOpt, args)

  bakDir = os.path.normpath(args["dir"])

  #
  # No errors found, continue.
  #

  logger.info("Restoring user data from dir: %s." % bakDir)

  PASS_NAME     = "passwd"
  PASS_FILE_BAK = os.path.join(bakDir, PASS_NAME)
  PASS_FILE     = os.path.join(comm.splunk_home, "etc", PASS_NAME)

  filename, oldFilePath, newFilePath = (PASS_NAME, PASS_FILE_BAK, PASS_FILE)
  if os.path.exists(oldFilePath):
    shutil.copy(oldFilePath, newFilePath)
  else:
    if filename in (PASS_NAME,):
      logger.info("No '%s' file found in supplied backup dir '%s'. Did you supply an incorrect directory?" % (filename, bakDir))

  try:
    importer = bundle_paths.BundlesImporter()
    site = bundle_paths.BundlesImportExportSite(bakDir)
    importer.do_import(site)
  except bundle_paths.BundleException, e:
    raise ce.FilePath, ("Unable to import: %s.  Aborting restore." % e)
def setPoll(args, fromCLI):
  paramsReq = (ARG_URI,)
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)

  ensureClientEnabled(fromCLI)
  return editClient({ARG_DEPSERVURI : args[ARG_URI]}, fromCLI)
def localModuleDisable(args, fromCLI):
  """
  Works only on the local filesystem.
  Disables module of given name.
  "module" param is path relative to etc/modules, with dirs separted by colons.
  """
  paramsReq = (ARG_MODULE,)
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}
  moduleName, modulePath = getNameAndPath(args[ARG_MODULE])
  currEnabled = os.path.exists(modulePath)

  if currEnabled: # then disable it
    returnDict["restartRequired"] = True
    disabledPath = getDisabledPath(modulePath)
    if os.path.exists(disabledPath):
      os.unlink(modulePath)
    else:
      shutil.move(modulePath, disabledPath)

  if fromCLI:
    if not currEnabled:
      logger.info("%s is already disabled." % moduleName)
    else:
      logger.info("%s disabled." % moduleName)
  return returnDict
def imUserSplunk(args, fromCLI):
  """
  Import users and splunks.
  """
  paramsReq = ("dir",)
  paramsOpt = ()

  comm.validateArgs(paramsReq, paramsOpt, args)

  bakDir = os.path.normpath(args["dir"])

  #
  # No errors found, continue.
  #

  logger.info("Restoring user data from dir: %s." % bakDir)

  PASS_NAME     = "passwd"
  PASS_FILE_BAK = os.path.join(bakDir, PASS_NAME)
  PASS_FILE     = os.path.join(comm.splunk_home, "etc", PASS_NAME)

  filename, oldFilePath, newFilePath = (PASS_NAME, PASS_FILE_BAK, PASS_FILE)
  if os.path.exists(oldFilePath):
    shutil.copy(oldFilePath, newFilePath)
  else:
    if filename in (PASS_NAME,):
      logger.info("No '%s' file found in supplied backup dir '%s'. Did you supply an incorrect directory?" % (filename, bakDir))

  try:
    importer = bundle_paths.BundlesImporter()
    site = bundle_paths.BundlesImportExportSite(bakDir)
    importer.do_import(site)
  except bundle_paths.BundleException, e:
    raise ce.FilePath, ("Unable to import: %s.  Aborting restore." % e)
Пример #7
0
def setLocalMgmtPort(args, fromCLI):
    paramReq = ("port", )
    paramOpt = ()
    comm.validateArgs(paramReq, paramOpt, args)
    if fromCLI:
        validate.checkPortNum("port", args["port"])

    retDict = {}
    host = "localhost"
    key = "mgmtHostPort"
    currSetts = comm.readConfFile(fullWebConfig)

    if not webSettingsStanza in currSetts:
        currSetts[webSettingsStanza] = {}
    if key in currSetts[webSettingsStanza]:
        try:
            # this expects the uri as "blah:8000" - or just "a:b", really.
            host, oldPortWhichIsGarbage = currSetts[webSettingsStanza][
                key].split(":", 1)
        except ValueError:
            pass  # leaves localhost value from above

    currSetts[webSettingsStanza][key] = "%s:%s" % (host, args["port"])
    comm.writeConfFile(fullWebConfig, currSetts)
    return retDict
Пример #8
0
def stanzaStatusInternal(args, fromCLI):
    """
  Returns boolean based on whether or not a given stanza in a given file has the "disabled" parameter set.
  """
    paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
    paramsOpt = (ARG_AUTHSTR, )
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {"enabled": False}
    stanza = args[ARG_STANZA]

    authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or None

    currStatus = comm.getMergedConf(args[ARG_CONFIG])

    if stanza in currStatus:
        returnDict[
            "enabled"] = True  # found the stanza, let's say it's enabled unless we learn otherwise.
        if KEY_DISABLED in currStatus[stanza]:
            if "true" == currStatus[stanza][KEY_DISABLED].lower():
                returnDict[
                    "enabled"] = False  # if disabled=true, override what we thought above.

    returnDict["stanzas"] = currStatus
    return returnDict
Пример #9
0
def i18n_extract(args, fromCLI):
    splunk_home = comm.splunk_home
    params_req = ('app', )
    params_opt = ()
    comm.validateArgs(params_req, params_opt, args)

    app_path = os.path.join(splunk_home, 'etc', 'apps', args['app'])
    app_locale_path = os.path.join(app_path, 'locale')
    if not os.path.exists(app_locale_path):
        os.makedirs(app_locale_path)

    messages_pot = os.path.join(app_locale_path, 'messages.pot')

    babel_cfg = os.path.join(app_locale_path, 'babel.cfg')
    if not os.path.exists(babel_cfg):
        from splunk.appserver.mrsparkle.lib import i18n
        mrsparkle = os.path.dirname(os.path.dirname(i18n.__file__))
        babel_cfg = os.path.join(mrsparkle, 'locale', 'babel.cfg')

    args = [
        'extract', '-F', babel_cfg, '-o', messages_pot, '-c', 'TRANS:',
        app_path
    ]

    sys.argv[1:] = args

    frontend.main()
Пример #10
0
def setInstanceName(argsDict, fromCLI):
    reqkeys = ('instancename', )
    optkeys = ()
    comm.validateArgs(reqkeys, optkeys, argsDict)
    returnDict = setLocalName({"name": argsDict["instancename"]}, fromCLI)
    returnDict["restartRequired"] = True
    return returnDict
Пример #11
0
def setMulticast(args, fromCLI):
    paramsReq = (ARG_URI, )
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)

    ensureClientEnabled(fromCLI)
    return editClient({ARG_MCASTURI: args[ARG_URI]}, fromCLI)
Пример #12
0
def exEvents(args, fromCLI):
    """
    Export events from the index.
    """
    paramsReq = ("dir", "index")
    paramsOpt = ("host", "source", "sourcetype", "terms")

    comm.validateArgs(paramsReq, paramsOpt, args)
    comm.requireSplunkdDown()

    args["dir"] = os.path.normpath(args["dir"])

    #
    # No errors found, continue.
    #

    index = args.pop("index")
    dest_path = args.pop("dir")

    i = 3
    query = []

    for metadataType in ("source", "sourcetype", "host"):
        if metadataType in args:
            query.append(metadataType + "::" + args[metadataType])

    # should this handle multi-word terms? question for sorkin. TODO
    if "terms" in args:
        for term in args["terms"].split(" "):
            query.append(term)

    scanAndExport(index, dest_path, query)
Пример #13
0
def localModuleDisable(args, fromCLI):
    """
  Works only on the local filesystem.
  Disables module of given name.
  "module" param is path relative to etc/modules, with dirs separted by colons.
  """
    paramsReq = (ARG_MODULE, )
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {}
    moduleName, modulePath = getNameAndPath(args[ARG_MODULE])
    currEnabled = os.path.exists(modulePath)

    if currEnabled:  # then disable it
        returnDict["restartRequired"] = True
        disabledPath = getDisabledPath(modulePath)
        if os.path.exists(disabledPath):
            os.unlink(modulePath)
        else:
            shutil.move(modulePath, disabledPath)

    if fromCLI:
        if not currEnabled:
            logger.info("%s is already disabled." % moduleName)
        else:
            logger.info("%s disabled." % moduleName)
    return returnDict
Пример #14
0
def importAllFlatFiles(args, fromCLI):
  """
  Flat file import.  Run it before startup.
  """
  paramsReq = ()
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)
  comm.requireSplunkdDown()

  #
  # No errors found, continue.
  #

  paths = []
  # be sure to exclude tempPath lines.  on *nix, at least, /tmp can contain all sorts
  # of things that you don't have permission to read, so the search-for-*.gz step will
  # cause python exceptions all over the place.  brian says don't bother checking it.
  regex = re.compile("(?!tempPath.*=)[^=]*Path[^=]*= *(.*)")
  # should make comm.grep() do the right thing here. TODO
  # build "paths" here by taking the right side of all lines that have Path in them.
  for potentialPath in subprocess.Popen("btool indexes list",
    shell=True, stdout=subprocess.PIPE).stdout:
    result = regex.match(potentialPath)
    if None != result: # found a match.
      # add first [and only] match, replace $SPLUNK_DB in it while we're at it.
      paths.append(os.path.expandvars(result.groups()[0].strip()))

  for onePath in paths:
    import glob
    for bucket in glob.iglob(os.path.join(onePath, "db_*_*_*")):
      for export_file in (EXPORT_FILE, EXPORT_GZ_FILE):
        export_file = os.path.join(bucket, export_file)
        if os.path.isfile(export_file):
          do_import(bucket, export_file)
          break
def localModuleEnable(args, fromCLI):
  """
  Works only on the local filesystem.
  Enables module of given name.
  "module" param is path relative to etc/modules, with dirs separted by colons.
  """
  paramsReq = (ARG_MODULE,)
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}
  moduleName, modulePath = getNameAndPath(args[ARG_MODULE])
  currEnabled = os.path.exists(modulePath)

  if not currEnabled: # then enable it
    returnDict["restartRequired"] = True
    disabledPath = getDisabledPath(modulePath)
    if not os.path.exists(disabledPath):
      raise ConfigError, "Could not find required file - looking for disabled config file at %s." % disabledPath
    shutil.copy(disabledPath, modulePath)

  if fromCLI:
    if currEnabled:
      logger.info("%s is already enabled." % moduleName)
    else:
      logger.info("%s enabled." % moduleName)
  return returnDict
def setInstanceName(argsDict, fromCLI):
    reqkeys = ('instancename',)
    optkeys = ()
    comm.validateArgs(reqkeys, optkeys, argsDict )
    returnDict = setLocalName({"name" : argsDict["instancename"]}, fromCLI)
    returnDict["restartRequired"] = True
    return returnDict
Пример #17
0
def exEvents(args, fromCLI):
    """
    Export events from the index.
    """
    paramsReq = ("dir", "index")
    paramsOpt = ("host", "source", "sourcetype", "terms")
    
    comm.validateArgs(paramsReq, paramsOpt, args)
    comm.requireSplunkdDown()
    
    args["dir"] = os.path.normpath(args["dir"])
    
    #
    # No errors found, continue.
    #

    index     = args.pop("index")
    dest_path = args.pop("dir")

    i = 3
    query = []

    for metadataType in ("source", "sourcetype", "host"):
        if metadataType in args:
            query.append(metadataType + "::" + args[metadataType])

    # should this handle multi-word terms? question for sorkin. TODO
    if "terms" in args:
        for term in args["terms"].split(" "):
            query.append(term)

    scanAndExport(index, dest_path, query)
Пример #18
0
def setMulticast(args, fromCLI):
  paramsReq = (ARG_URI,)
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)

  ensureClientEnabled(fromCLI)
  return editClient({ARG_MCASTURI : args[ARG_URI]}, fromCLI)
Пример #19
0
def localModuleEnable(args, fromCLI):
    """
  Works only on the local filesystem.
  Enables module of given name.
  "module" param is path relative to etc/modules, with dirs separted by colons.
  """
    paramsReq = (ARG_MODULE, )
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {}
    moduleName, modulePath = getNameAndPath(args[ARG_MODULE])
    currEnabled = os.path.exists(modulePath)

    if not currEnabled:  # then enable it
        returnDict["restartRequired"] = True
        disabledPath = getDisabledPath(modulePath)
        if not os.path.exists(disabledPath):
            raise ConfigError, "Could not find required file - looking for disabled config file at %s." % disabledPath
        shutil.copy(disabledPath, modulePath)

    if fromCLI:
        if currEnabled:
            logger.info("%s is already enabled." % moduleName)
        else:
            logger.info("%s enabled." % moduleName)
    return returnDict
Пример #20
0
def preFlightChecks(args, fromCLI=False):
    paramReq = ()
    paramOpt = ()
    comm.validateArgs(paramReq, paramOpt, args)
    #
    checkPerms()
    index.importAllFlatFiles({}, fromCLI)
    checkSearchthing()
    checkSELinux()
Пример #21
0
def preFlightChecks(args, fromCLI = False):
  paramReq = ()
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  #
  checkPerms()
  index.importAllFlatFiles({}, fromCLI)
  checkSearchthing()
  checkSELinux()
Пример #22
0
def getMgmtURL(argsDict, fromCLI):
    reqkeys = ('authstr', )
    optkeys = ()

    # we take only one optional arg
    comm.validateArgs(reqkeys, optkeys, argsDict)
    returnDict = {}
    returnDict["mgmturl"] = comm.getWebConfKeyValue("mgmtHostPort")

    return returnDict
def getMgmtURL(argsDict, fromCLI):
    reqkeys = ('authstr',)
    optkeys = ()
    
    # we take only one optional arg
    comm.validateArgs(reqkeys, optkeys, argsDict )
    returnDict = {}
    returnDict["mgmturl"] = comm.getWebConfKeyValue("mgmtHostPort")
    
    return returnDict
Пример #24
0
def editClient(args, fromCLI):
    """
  Edits the various options of a deployment client.
  THIS WORKS ON THE LOCAL FILESYSTEM.  We usually don't want this,
  so this will only be used for certain options.
  """
    paramsReq = ()
    paramsOpt = tuple(optionMapClient.keys())
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {}

    if 0 == len(args):
        raise cex.ArgError, "No changes have been specified."

    if not module.deplClientStatus({}, fromCLI)["enabled"]:
        raise cex.ServerState, ERR_CLIENT_DIS

    currConf = comm.getMergedConf(module.DEPL_CLI_CONFIG)
    if not module.DEPL_CLI_STANZA in currConf:
        raise cex.ParsingError, ERR_CLI_STANZA

    # assuming that we only support one of each of these tags - replace every tag found, and add if non-existent.
    for arg, val in args.items():
        paramName = optionMapClient[arg]

        # validate the few types of args we recognize, ignore anything else.
        try:
            if arg in (ARG_MCASTURI, ARG_DEPSERVURI):
                validate.checkIPPortOrHostPort(arg, val)
            elif arg == ARG_MCASTIP:
                validate.checkIP(arg, val)
            elif arg == ARG_POLLFREQ:
                validate.checkPosInt(arg, val)
        except cex.ValidationError:
            if "0" != val:  # we use 0 to disable these things, i guess.
                raise

        # remove if 0.
        if "0" == val and paramName in currConf[module.DEPL_CLI_STANZA]:
            currConf[module.DEPL_CLI_STANZA].pop(paramName)
        # or add/set.
        else:
            currConf[module.DEPL_CLI_STANZA][paramName] = val

        # if we're at this point, *something* has changed.
        returnDict["restartRequired"] = True

    comm.writeConfFile(
        bundle_paths.make_path(module.DEPL_CLI_CONFIG + ".conf"), currConf)
    if fromCLI:
        logger.info("Configuration updated.")

    return returnDict
Пример #25
0
def deplClientStatus(args, fromCLI):
    """Checks whether or not deployment client module is enabled."""
    paramsReq = ()
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)
    return stanzaStatus(
        {
            ARG_CONFIG: DEPL_CLI_CONFIG,
            ARG_MODNAME: DEPL_CLIENT_NAME,
            ARG_STANZA: DEPL_CLI_STANZA
        }, fromCLI)
Пример #26
0
def getUIVersion(argsDict, fromCLI):
    '''DEPRECATED: uiversion switching is no longer supported'''
    paramsReq = ()
    paramsOpt = ()

    comm.validateArgs(paramsReq, paramsOpt, argsDict)
    retDict = {'uiversion': 'DEPRECATED'}

    if fromCLI:
        logger.warn("Web UI version setting has been deprecated")
    return retDict
def getUIVersion(argsDict, fromCLI):
  '''DEPRECATED: uiversion switching is no longer supported'''
  paramsReq = ()
  paramsOpt = ()

  comm.validateArgs(paramsReq, paramsOpt, argsDict)
  retDict   = {'uiversion': 'DEPRECATED'}
  
  if fromCLI:
    logger.warn("Web UI version setting has been deprecated")
  return retDict
Пример #28
0
def editClient(args, fromCLI):
  """
  Edits the various options of a deployment client.
  THIS WORKS ON THE LOCAL FILESYSTEM.  We usually don't want this,
  so this will only be used for certain options.
  """
  paramsReq = ()
  paramsOpt = tuple(optionMapClient.keys())
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}

  if 0 == len(args):
    raise cex.ArgError, "No changes have been specified."

  if not module.deplClientStatus({}, fromCLI)["enabled"]:
    raise cex.ServerState, ERR_CLIENT_DIS

  
  currConf = comm.getMergedConf(module.DEPL_CLI_CONFIG)
  if not module.DEPL_CLI_STANZA in currConf:
    raise cex.ParsingError, ERR_CLI_STANZA

  # assuming that we only support one of each of these tags - replace every tag found, and add if non-existent.
  for arg, val in args.items():
    paramName = optionMapClient[arg]

    # validate the few types of args we recognize, ignore anything else.
    try:
      if arg in (ARG_MCASTURI, ARG_DEPSERVURI):
        validate.checkIPPortOrHostPort(arg, val)
      elif arg == ARG_MCASTIP:
        validate.checkIP(arg, val)
      elif arg == ARG_POLLFREQ:
        validate.checkPosInt(arg, val)
    except cex.ValidationError:
      if "0" != val: # we use 0 to disable these things, i guess.
        raise

    # remove if 0.
    if "0" == val and paramName in currConf[module.DEPL_CLI_STANZA]:
      currConf[module.DEPL_CLI_STANZA].pop(paramName)
    # or add/set.
    else:
      currConf[module.DEPL_CLI_STANZA][paramName] = val

    # if we're at this point, *something* has changed.
    returnDict["restartRequired"] = True

  comm.writeConfFile(bundle_paths.make_path(module.DEPL_CLI_CONFIG + ".conf"), currConf)
  if fromCLI:
    logger.info("Configuration updated.")

  return returnDict
Пример #29
0
def checkXmlFiles(args, fromCLI):
    """
  Gathers all .xml files in path, recursively, and runs them through the
  minidom parser.
  """

    print "\tChecking configuration... ",
    sys.stdout.flush()

    paramReq = ()
    paramOpt = ()
    comm.validateArgs(paramReq, paramOpt, args)

    retDict = {}

    ################
    ################ BEGIN PART 1
    ################

    xmlErrs = 0
    xmlFiles = []
    # path to search.
    dirPath = os.path.join(comm.splunk_home, "etc")
    # gather .xml files into xmlFiles.
    xmlFiles = comm.findFiles(dirPath, "\\.xml$", caseSens=False, minBytes=10)

    # run the cmd on appropriately sized subsets of xml files.
    for file in xmlFiles:

        # should not start with "." (editor temp, hidden, etc) because dotfiles are lame
        # this is only enforced via this pre-flight message at the moment (4.2.2)
        if os.path.basename(file).startswith("."):
            logger.error("\nIgnored file '%s': filename begins with '.'\n",
                         file)
            continue

        try:
            xml.dom.minidom.parse(file)
        except:
            value = sys.exc_info()[1]
            logger.error("Error while parsing '%s':\n %s\n", file, value)
            xmlErrs += 1

    ### all done, report/prompt now.

    if xmlErrs > 0:
        if not comm.prompt_user(
                "\nThere were problems with the configuration files.\nWould you like to ignore these errors? [y/n]:"
        ):
            raise cex.ParsingError, "Parsing error in configuration files."

    print "Done."
    return retDict
Пример #30
0
def deplClientDisable(args, fromCLI):
    """Enables the deployment client if currently enabled."""
    paramsReq = ()
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)
    return stanzaDisable(
        {
            ARG_CONFIG: DEPL_CLI_CONFIG,
            ARG_MODNAME: DEPL_CLIENT_NAME,
            ARG_STANZA: DEPL_CLI_STANZA,
            ARG_USEFS: "true"
        }, fromCLI)
Пример #31
0
def checkXmlFiles(args, fromCLI):
  """
  Gathers all .xml files in path, recursively, and runs them through the
  minidom parser.
  """

  print "\tChecking configuration... ",; sys.stdout.flush()


  paramReq = ()
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)

  retDict  = {}

  ################
  ################ BEGIN PART 1
  ################

  xmlErrs  = 0
  xmlFiles = []
  # path to search.
  dirPath  = os.path.join(comm.splunk_home, "etc")
  # gather .xml files into xmlFiles. 
  xmlFiles = comm.findFiles(dirPath, "\\.xml$", caseSens = False, minBytes = 10)

  # run the cmd on appropriately sized subsets of xml files.
  for file in xmlFiles:

    # should not start with "." (editor temp, hidden, etc) because dotfiles are lame
    # this is only enforced via this pre-flight message at the moment (4.2.2)
    if os.path.basename(file).startswith("."):
      logger.error("\nIgnored file '%s': filename begins with '.'\n", file)
      continue

    try:
      xml.dom.minidom.parse(file)
    except:
      value = sys.exc_info()[1]
      logger.error("Error while parsing '%s':\n %s\n", file, value)
      xmlErrs += 1

  ### all done, report/prompt now.

  if xmlErrs > 0:
    if not comm.prompt_user("\nThere were problems with the configuration files.\nWould you like to ignore these errors? [y/n]:"):
      raise cex.ParsingError, "Parsing error in configuration files."

  print "Done." 
  return retDict
Пример #32
0
def setLocalName(args, fromCLI):
    paramReq = ("name", )
    paramOpt = ()
    comm.validateArgs(paramReq, paramOpt, args)
    name = args["name"]
    if name.count(" ") + name.count(":") > 0:
        raise ArgError, "Name '%s' is invalid.  Names cannot contain spaces or colons." % name

    currSetts = comm.readConfFile(fullServerConfig)
    if not serverGeneralStanza in currSetts:
        currSetts[serverGeneralStanza] = {}
    currSetts[serverGeneralStanza]["serverName"] = name
    comm.writeConfFile(fullServerConfig, currSetts)
    return {}
def setLocalName(args, fromCLI):
  paramReq = ("name",)
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  name = args["name"]
  if name.count(" ") + name.count(":") > 0:
    raise ArgError, "Name '%s' is invalid.  Names cannot contain spaces or colons." % name

  currSetts = comm.readConfFile(fullServerConfig)
  if not serverGeneralStanza in currSetts:
    currSetts[serverGeneralStanza] = {}
  currSetts[serverGeneralStanza]["serverName"] = name
  comm.writeConfFile(fullServerConfig, currSetts)
  return {}
Пример #34
0
def stanzaStatus(args, fromCLI):
    """
  Wrapper that prints stuff.
  """
    paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
    paramsOpt = (ARG_AUTHSTR, )
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = stanzaStatusInternal(args, fromCLI)
    if fromCLI:
        if returnDict["enabled"]:
            logger.info("%s is enabled." % args[ARG_MODNAME])
        else:
            logger.info("%s is disabled." % args[ARG_MODNAME])
    return returnDict
def stanzaStatus(args, fromCLI):
  """
  Wrapper that prints stuff.
  """
  paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
  paramsOpt = (ARG_AUTHSTR,)
  comm.validateArgs(paramsReq, paramsOpt, args)
  
  returnDict = stanzaStatusInternal(args, fromCLI)
  if fromCLI:
    if returnDict["enabled"]:
      logger.info("%s is enabled." % args[ARG_MODNAME])
    else:
      logger.info("%s is disabled." % args[ARG_MODNAME])
  return returnDict
def getInstanceName(argsDict, fromCLI):
    reqkeys = ()
    optkeys = ()
    returnDict = {}
    comm.validateArgs(reqkeys, optkeys, argsDict )

    currSetts = comm.readConfFile(fullServerConfig)
    returnDict["instancename"] = "(unset)";
    if serverGeneralStanza in currSetts:
      if "serverName" in currSetts[serverGeneralStanza]:
        returnDict["instancename"] = currSetts[serverGeneralStanza]["serverName"]

    if fromCLI:
      logger.info("Server name: %s." % returnDict["instancename"])
    return returnDict
Пример #37
0
def setDef(args, fromCLI):
  """
  Set the new default index.
  """
  paramsReq = ("value",)
  paramsOpt = ()

  comm.validateArgs(paramsReq, paramsOpt, args)
  comm.requireSplunkdDown()

  #
  # No errors found, continue.
  #

  os.system("python \"%s\" --default \"%s\"" % (DB_MANIP, args["value"]))
Пример #38
0
def getDef(args, fromCLI):
  """
  Show the default index.
  """
  paramsReq = ()
  paramsOpt = ()

  comm.validateArgs(paramsReq, paramsOpt, args)
  comm.requireSplunkdDown()

  #
  # No errors found, continue.
  #

  logger.info("Default index: ")
  os.system("python \"%s\" --showdefault" % DB_MANIP)
Пример #39
0
def migrate_bundles(args, fromCLI):
    ARG_DRYRUN = "dry-run"
    ARG_NAME = "name"
    comm.validateArgs((), (ARG_NAME, ARG_DRYRUN), args)
    isDryRun = comm.getBoolValue(ARG_DRYRUN, args.get(ARG_DRYRUN, "false"))
    name = args.get(ARG_NAME)
    found = False
    logger.info(lit("INFO_MIGRATE_START__S") % name)
    for b in bundles_iterator(True):
        if b.is_legacy() and ((name is None) or (name == b.name())):
            b.migrate(isDryRun)
            found = True
    if name and (not found):
        raise cex.ArgError, "Cannot find a legacy bundle named '%s'." % name
    else:
        logger.info(lit("INFO_MIGRATE_END__S") % name)
Пример #40
0
def getInstanceName(argsDict, fromCLI):
    reqkeys = ()
    optkeys = ()
    returnDict = {}
    comm.validateArgs(reqkeys, optkeys, argsDict)

    currSetts = comm.readConfFile(fullServerConfig)
    returnDict["instancename"] = "(unset)"
    if serverGeneralStanza in currSetts:
        if "serverName" in currSetts[serverGeneralStanza]:
            returnDict["instancename"] = currSetts[serverGeneralStanza][
                "serverName"]

    if fromCLI:
        logger.info("Server name: %s." % returnDict["instancename"])
    return returnDict
Пример #41
0
def migrate_bundles(args, fromCLI):
    ARG_DRYRUN = "dry-run"
    ARG_NAME = "name"
    comm.validateArgs(( ), (ARG_NAME, ARG_DRYRUN), args)
    isDryRun = comm.getBoolValue(ARG_DRYRUN, args.get(ARG_DRYRUN, "false"))
    name = args.get(ARG_NAME)
    found = False
    logger.info(lit("INFO_MIGRATE_START__S") % name)
    for b in bundles_iterator(True):
        if b.is_legacy() and ((name is None) or (name == b.name())):
            b.migrate(isDryRun)
            found = True
    if name and (not found):
        raise cex.ArgError, "Cannot find a legacy bundle named '%s'." % name
    else:
        logger.info(lit("INFO_MIGRATE_END__S") % name)
Пример #42
0
def stanzaDisable(args, fromCLI):
    """
  Disables a given stanza in a given conf file.
  """
    paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
    paramsOpt = (ARG_AUTHSTR, ARG_USEFS)
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {}
    stanza = args[ARG_STANZA]
    confFile = bundle_paths.make_path(args[ARG_CONFIG] + ".conf")

    authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or ""

    # see if it's currently enabled.
    currStatus = stanzaStatusInternal(
        {
            ARG_CONFIG: args[ARG_CONFIG],
            ARG_MODNAME: args[ARG_MODNAME],
            ARG_STANZA: stanza,
            ARG_AUTHSTR: authStr
        }, fromCLI)
    currEnabled = currStatus["enabled"]

    # get the local .conf file.  don't use the stanzas given to us from "status",
    # because they're merged - we don't want to write out all the merged settings.
    # btw, readConfFile checks to see if the path exists and does the right thing.
    confStanzas = comm.readConfFile(confFile)

    if currEnabled:  # then disable it
        returnDict["restartRequired"] = True
        # create the stanza if it's not in the .conf already (could be on via default bundle)
        if not stanza in confStanzas:
            confStanzas[stanza] = {}
        # and make sure we set the disabled key in local to be true.
        confStanzas[stanza][KEY_DISABLED] = "true"
        stanzaXml = comm.flatDictToXML(confStanzas[stanza])
        # the following is now always true:
        # in order to support "splunk set deploy-poll", we allow this stuff to also write to the local filesystem.
        logger.debug("Attempting to disable module via local filesystem.")
        comm.writeConfFile(confFile, confStanzas)

    if not currEnabled:
        logger.info("%s is already disabled." % args[ARG_MODNAME])
    else:
        logger.info("%s disabled." % args[ARG_MODNAME])
    return returnDict
Пример #43
0
def setLocalHttp(args, fromCLI):
    paramReq = ("port", )
    paramOpt = ()
    comm.validateArgs(paramReq, paramOpt, args)
    if fromCLI:
        validate.checkPortNum("port", args["port"])

    retDict = {}
    key = "httpport"
    currSetts = comm.readConfFile(fullWebConfig)

    if not webSettingsStanza in currSetts:
        currSetts[webSettingsStanza] = {}
    currSetts[webSettingsStanza][key] = args["port"]

    comm.writeConfFile(fullWebConfig, currSetts)
    return retDict
def setLocalHttp(args, fromCLI):
  paramReq = ("port",)
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  if fromCLI:
    validate.checkPortNum("port", args["port"])

  retDict = {}
  key = "httpport"
  currSetts = comm.readConfFile(fullWebConfig)

  if not webSettingsStanza in currSetts:
    currSetts[webSettingsStanza] = {}
  currSetts[webSettingsStanza][key] = args["port"]

  comm.writeConfFile(fullWebConfig, currSetts)
  return retDict
Пример #45
0
def stanzaEnable(args, fromCLI):
    """
  Enables a given stanza in a given conf file.
  """
    paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
    paramsOpt = (ARG_AUTHSTR, ARG_USEFS)
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {}
    authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or ""
    stanza = args[ARG_STANZA]
    confFile = bundle_paths.make_path(args[ARG_CONFIG] + ".conf")
    currStatus = stanzaStatusInternal(
        {
            ARG_CONFIG: args[ARG_CONFIG],
            ARG_MODNAME: args[ARG_MODNAME],
            ARG_STANZA: stanza,
            ARG_AUTHSTR: authStr
        }, fromCLI)
    currEnabled = currStatus["enabled"]

    # get the local .conf file.  don't use the stanzas given to us from "status",
    # because they're merged - we don't want to write out all the merged settings.
    # btw, readConfFile checks to see if the path exists and does the right thing.
    confStanzas = comm.readConfFile(confFile)

    if not currEnabled:  # then enable it
        returnDict["restartRequired"] = True
        # create the stanza if it's not in the .conf already (if it was never enabled)
        if not stanza in confStanzas:
            confStanzas[stanza] = {}
        # at this point the only way for it to be disabled is for the disabled key to be true.
        # set it false regardless of whether or not it exists.
        confStanzas[stanza][KEY_DISABLED] = "false"
        stanzaXml = comm.flatDictToXML(confStanzas[stanza])
        # the following is now always true:
        # if applicable, just write to the local FS (used by splunk set deploy-poll).
        logger.debug("Attempting to enable module via local filesystem.")
        comm.writeConfFile(confFile, confStanzas)

    if currEnabled:
        logger.info("%s is already enabled." % args[ARG_MODNAME])
    else:
        logger.info("%s enabled." % args[ARG_MODNAME])
    return returnDict
def getPoll(args, fromCLI):
  paramsReq = ()
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)

  retDict  = {"enabled" : False}
  currConf = comm.getMergedConf(module.DEPL_CLI_CONFIG)
  try:
    retDict["uri"] = currConf[module.DEPL_CLI_STANZA][XML_DEPSERVURI]
    retDict["enabled"] = True
  except KeyError:
    pass

  if fromCLI:
    if retDict["enabled"]:
      logger.info("Deployment Server URI is set to \"%s\"." % retDict["uri"])
    else:
      logger.info("Deployment Server URI is not set.")
  return retDict
Пример #47
0
def getMulticast(args, fromCLI):
    paramsReq = ()
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)

    retDict = {"enabled": False}
    currConf = comm.getMergedConf(module.DEPL_CLI_CONFIG)
    try:
        retDict["uri"] = currConf[module.DEPL_CLI_STANZA][XML_MCASTURI]
        retDict["enabled"] = True
    except KeyError:
        pass

    if fromCLI:
        if retDict["enabled"]:
            logger.info("Multicast URI is set to \"%s\"." % retDict["uri"])
        else:
            logger.info("Multicast URI is not set.")
    return retDict
def stanzaDisable(args, fromCLI):
  """
  Disables a given stanza in a given conf file.
  """
  paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
  paramsOpt = (ARG_AUTHSTR, ARG_USEFS)
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}
  stanza = args[ARG_STANZA]
  confFile = bundle_paths.make_path(args[ARG_CONFIG] + ".conf")

  authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or ""

  # see if it's currently enabled.
  currStatus = stanzaStatusInternal({ARG_CONFIG : args[ARG_CONFIG], ARG_MODNAME : args[ARG_MODNAME], ARG_STANZA : stanza, ARG_AUTHSTR: authStr}, fromCLI)
  currEnabled = currStatus["enabled"]

  # get the local .conf file.  don't use the stanzas given to us from "status",
  # because they're merged - we don't want to write out all the merged settings.
  # btw, readConfFile checks to see if the path exists and does the right thing.
  confStanzas = comm.readConfFile(confFile)

  if currEnabled: # then disable it
    returnDict["restartRequired"] = True
    # create the stanza if it's not in the .conf already (could be on via default bundle)
    if not stanza in confStanzas:
      confStanzas[stanza] = {}
    # and make sure we set the disabled key in local to be true.
    confStanzas[stanza][KEY_DISABLED] = "true"
    stanzaXml = comm.flatDictToXML(confStanzas[stanza]) 
    # the following is now always true:
    # in order to support "splunk set deploy-poll", we allow this stuff to also write to the local filesystem.
    logger.debug("Attempting to disable module via local filesystem.")
    comm.writeConfFile(confFile, confStanzas)

  if not currEnabled:
    logger.info("%s is already disabled." % args[ARG_MODNAME])
  else:
    logger.info("%s disabled." % args[ARG_MODNAME])
  return returnDict
def localModuleStatus(args, fromCLI):
  """
  Works only on the local filesystem.
  Returns boolean based on whether or not module of given name is enabled.
  "module" param is path relative to etc/modules, with dirs separted by colons.
  """
  paramsReq = (ARG_MODULE,)
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {"enabled" : False}
  moduleName, modulePath = getNameAndPath(args[ARG_MODULE])
  if os.path.exists(modulePath):
    returnDict["enabled"] = True

  if fromCLI:
    if returnDict["enabled"]:
      logger.info("%s is enabled." % moduleName)
    else:
      logger.info("%s is disabled." % moduleName)
  return returnDict
Пример #50
0
def localModuleStatus(args, fromCLI):
    """
  Works only on the local filesystem.
  Returns boolean based on whether or not module of given name is enabled.
  "module" param is path relative to etc/modules, with dirs separted by colons.
  """
    paramsReq = (ARG_MODULE, )
    paramsOpt = ()
    comm.validateArgs(paramsReq, paramsOpt, args)

    returnDict = {"enabled": False}
    moduleName, modulePath = getNameAndPath(args[ARG_MODULE])
    if os.path.exists(modulePath):
        returnDict["enabled"] = True

    if fromCLI:
        if returnDict["enabled"]:
            logger.info("%s is enabled." % moduleName)
        else:
            logger.info("%s is disabled." % moduleName)
    return returnDict
def stanzaEnable(args, fromCLI):
  """
  Enables a given stanza in a given conf file.
  """
  paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
  paramsOpt = (ARG_AUTHSTR, ARG_USEFS)
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {}
  authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or ""
  stanza = args[ARG_STANZA]
  confFile = bundle_paths.make_path(args[ARG_CONFIG] + ".conf")
  currStatus = stanzaStatusInternal({ARG_CONFIG : args[ARG_CONFIG], ARG_MODNAME : args[ARG_MODNAME], ARG_STANZA : stanza, ARG_AUTHSTR: authStr}, fromCLI)
  currEnabled = currStatus["enabled"]

  # get the local .conf file.  don't use the stanzas given to us from "status",
  # because they're merged - we don't want to write out all the merged settings.
  # btw, readConfFile checks to see if the path exists and does the right thing.
  confStanzas = comm.readConfFile(confFile)

  if not currEnabled: # then enable it
    returnDict["restartRequired"] = True
    # create the stanza if it's not in the .conf already (if it was never enabled)
    if not stanza in confStanzas:
      confStanzas[stanza] = {}
    # at this point the only way for it to be disabled is for the disabled key to be true.
    # set it false regardless of whether or not it exists.
    confStanzas[stanza][KEY_DISABLED] = "false"
    stanzaXml = comm.flatDictToXML(confStanzas[stanza]) 
    # the following is now always true:
    # if applicable, just write to the local FS (used by splunk set deploy-poll).
    logger.debug("Attempting to enable module via local filesystem.")
    comm.writeConfFile(confFile, confStanzas)

  if currEnabled:
    logger.info("%s is already enabled." % args[ARG_MODNAME])
  else:
    logger.info("%s enabled." % args[ARG_MODNAME])
  return returnDict
def stanzaStatusInternal(args, fromCLI):
  """
  Returns boolean based on whether or not a given stanza in a given file has the "disabled" parameter set.
  """
  paramsReq = (ARG_CONFIG, ARG_MODNAME, ARG_STANZA)
  paramsOpt = (ARG_AUTHSTR,)
  comm.validateArgs(paramsReq, paramsOpt, args)

  returnDict = {"enabled" : False}
  stanza = args[ARG_STANZA]

  authStr = (ARG_AUTHSTR in args) and args[ARG_AUTHSTR] or None

  currStatus = comm.getMergedConf(args[ARG_CONFIG])

  if stanza in currStatus:
    returnDict["enabled"] = True # found the stanza, let's say it's enabled unless we learn otherwise.
    if KEY_DISABLED in currStatus[stanza]:
      if "true" == currStatus[stanza][KEY_DISABLED].lower():
        returnDict["enabled"] = False # if disabled=true, override what we thought above.

  returnDict["stanzas"] = currStatus
  return returnDict
Пример #53
0
def exUserSplunk(args, fromCLI):
    """
  Export users and splunks.
  """
    paramsReq = ("dir", )
    paramsOpt = ()

    comm.validateArgs(paramsReq, paramsOpt, args)

    bakDir = os.path.normpath(args["dir"])

    #
    # No errors found, continue.
    #

    logger.info("Backing up to dir: %s." % bakDir)

    PASS_NAME = "passwd"
    PASS_FILE_BAK = os.path.join(bakDir, PASS_NAME)
    PASS_FILE = os.path.join(comm.splunk_home, "etc", PASS_NAME)

    if not os.path.isdir(bakDir):
        os.makedirs(bakDir)

    if not os.path.exists(PASS_FILE):
        raise FilePath, "Unable to find user file '%s', please ensure that SPLUNK_HOME is defined correctly (currently: %s).  Aborting backup." % (
            PASS_FILE, comm.splunk_home)

    shutil.copy(PASS_FILE, PASS_FILE_BAK)

    try:
        targetFiles = ["savedsearches.conf", "eventtypes.conf"]
        exporter = bundle_paths.BundlesExporter()
        site = bundle_paths.BundlesImportExportSite(bakDir)
        exporter.do_export(targetFiles, site)
    except bundle_paths.BundleException, e:
        raise FilePath, ("Unable to export: %s.  Aborting backup." % e)
def setLocalMgmtPort(args, fromCLI):
  paramReq = ("port",)
  paramOpt = ()
  comm.validateArgs(paramReq, paramOpt, args)
  if fromCLI:
    validate.checkPortNum("port", args["port"])

  retDict = {}
  host = "localhost"
  key  = "mgmtHostPort"
  currSetts = comm.readConfFile(fullWebConfig)

  if not webSettingsStanza in currSetts:
    currSetts[webSettingsStanza] = {}
  if key in currSetts[webSettingsStanza]:
    try:
      # this expects the uri as "blah:8000" - or just "a:b", really.
      host, oldPortWhichIsGarbage = currSetts[webSettingsStanza][key].split(":", 1)
    except ValueError:
      pass # leaves localhost value from above

  currSetts[webSettingsStanza][key] = "%s:%s" % (host, args["port"])
  comm.writeConfFile(fullWebConfig, currSetts)
  return retDict
Пример #55
0
def exUserSplunk(args, fromCLI):
  """
  Export users and splunks.
  """
  paramsReq = ("dir",)
  paramsOpt = ()

  comm.validateArgs(paramsReq, paramsOpt, args)

  bakDir = os.path.normpath(args["dir"])

  #
  # No errors found, continue.
  #

  logger.info("Backing up to dir: %s." % bakDir)

  PASS_NAME     = "passwd"
  PASS_FILE_BAK = os.path.join(bakDir, PASS_NAME)
  PASS_FILE     = os.path.join(comm.splunk_home, "etc", PASS_NAME)

  if not os.path.isdir(bakDir):
    os.makedirs(bakDir)

  if not os.path.exists(PASS_FILE):
    raise FilePath, "Unable to find user file '%s', please ensure that SPLUNK_HOME is defined correctly (currently: %s).  Aborting backup." % (PASS_FILE, comm.splunk_home)

  shutil.copy(PASS_FILE, PASS_FILE_BAK)

  try:
    targetFiles = ["savedsearches.conf", "eventtypes.conf"]
    exporter = bundle_paths.BundlesExporter()
    site = bundle_paths.BundlesImportExportSite(bakDir)
    exporter.do_export(targetFiles, site)
  except bundle_paths.BundleException, e:
    raise FilePath, ("Unable to export: %s.  Aborting backup." % e)
Пример #56
0
def testSourcetypes(args, fromCLI):
  paramsReq = (ARG_FILE,)
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)
  os.system("classify \"%s\"" % args[ARG_FILE])
Пример #57
0
def testFields(args, fromCLI):
  paramsReq = ()
  paramsOpt = ()
  comm.validateArgs(paramsReq, paramsOpt, args)
  os.system(makeScriptCmd("interactiveLearner.py"))