Пример #1
0
def verifyHelper(filename, dict, fix_rrd=False):
    """
    Helper function for verifyRRD.  Checks one file,
    prints out errors.  if fix_rrd, will attempt to
    dump out rrd to xml, add the missing attributes,
    then restore.  Original file is obliterated.

    @param filename: filename of rrd to check
    @param dict: expected dictionary
    @param fix_rrd: if true, will attempt to add missing attrs
    """
    global rrd_problems_found
    if not os.path.exists(filename):
        print("WARNING: %s missing, will be created on restart" % (filename))
        return
    rrd_obj = rrdSupport.rrdSupport()
    (missing, extra) = rrd_obj.verify_rrd(filename, dict)
    for attr in extra:
        print("ERROR: %s has extra attribute %s" % (filename, attr))
        if fix_rrd:
            print("ERROR: fix_rrd cannot fix extra attributes")
    if not fix_rrd:
        for attr in missing:
            print("ERROR: %s missing attribute %s" % (filename, attr))
        if len(missing) > 0:
            rrd_problems_found = True
    if fix_rrd and (len(missing) > 0):
        (f, tempfilename) = tempfile.mkstemp()
        (out, tempfilename2) = tempfile.mkstemp()
        (restored, restoredfilename) = tempfile.mkstemp()
        backup_str = str(int(time.time())) + ".backup"
        print("Fixing %s... (backed up to %s)" %
              (filename, filename + backup_str))
        os.close(out)
        os.close(restored)
        os.unlink(restoredfilename)
        # Use exe version since dump, restore not available in rrdtool
        dump_obj = rrdSupport.rrdtool_exe()
        outstr = dump_obj.dump(filename)
        for line in outstr:
            os.write(f, "%s\n" % line)
        os.close(f)
        # Move file to backup location
        shutil.move(filename, filename + backup_str)
        rrdSupport.addDataStore(tempfilename, tempfilename2, missing)
        outstr = dump_obj.restore(tempfilename2, restoredfilename)
        os.unlink(tempfilename)
        os.unlink(tempfilename2)
        shutil.move(restoredfilename, filename)
    if len(extra) > 0:
        rrd_problems_found = True
def verifyHelper(filename,dict, fix_rrd=False):
    """
    Helper function for verifyRRD.  Checks one file,
    prints out errors.  if fix_rrd, will attempt to
    dump out rrd to xml, add the missing attributes,
    then restore.  Original file is obliterated.

    @param filename: filename of rrd to check
    @param dict: expected dictionary
    @param fix_rrd: if true, will attempt to add missing attrs
    """
    global rrd_problems_found
    if not os.path.exists(filename):
        print "WARNING: %s missing, will be created on restart" % (filename)
        return
    rrd_obj=rrdSupport.rrdSupport()
    (missing,extra)=rrd_obj.verify_rrd(filename,dict)
    for attr in extra:
        print "ERROR: %s has extra attribute %s" % (filename,attr)
        if fix_rrd:
            print "ERROR: fix_rrd cannot fix extra attributes"
    if not fix_rrd:
        for attr in missing:
            print "ERROR: %s missing attribute %s" % (filename,attr)
        if len(missing) > 0:
            rrd_problems_found=True
    if fix_rrd and (len(missing) > 0):
        (f,tempfilename)=tempfile.mkstemp()
        (out,tempfilename2)=tempfile.mkstemp()
        (restored,restoredfilename)=tempfile.mkstemp()
        backup_str=str(int(time.time()))+".backup"
        print "Fixing %s... (backed up to %s)" % (filename,filename+backup_str)
        os.close(out)
        os.close(restored)
        os.unlink(restoredfilename)
        #Use exe version since dump, restore not available in rrdtool
        dump_obj=rrdSupport.rrdtool_exe()
        outstr=dump_obj.dump(filename)
        for line in outstr:
            os.write(f,"%s\n"%line)
        os.close(f)
        #Move file to backup location 
        shutil.move(filename,filename+backup_str)
        rrdSupport.addDataStore(tempfilename,tempfilename2,missing)
        outstr=dump_obj.restore(tempfilename2,restoredfilename)
        os.unlink(tempfilename)
        os.unlink(tempfilename2)
        shutil.move(restoredfilename,filename)
    if len(extra) > 0:
        rrd_problems_found=True