def main(argv): if os.name == 'nt': cfgpath = os.path.expandvars('%APPDATA%\IPhistdb') elif os.name == 'posix': cfgpath = '/etc/IPhistdb' cfg = Config(os.path.join(cfgpath, 'processcnr.ini')) logging.config.fileConfig(os.path.join(cfgpath, 'processcnr.logging.ini')) logger = logging.getLogger('file') logger.debug('INIT') try: lock(cfg.getpath('App', 'lockfile')) try: db = AggDB() db.connect(cfg.get('AggDB', 'host'), cfg.get('AggDB', 'user'), cfg.get('AggDB', 'pass'), cfg.get('AggDB', 'db')) iphist = Iphist(cfg.get('CNR', 'iphist'), cfg.get('CNR', 'user'), cfg.get('CNR', 'pass')) for lease in iphist.leases('all'): try: db.addlease(lease) logger.debug(' '.join(['addlease', lease.ip, lease.start.strftime('%Y-%m-%d %H:%M:%S'), lease.end.strftime('%Y-%m-%d %H:%M:%S')])) except ParseException as e: logger.warning(e) nrcmd = Nrcmd(cfg.get('CNR', 'nrcmd'), cfg.get('CNR', 'user'), cfg.get('CNR', 'pass')) nrcmd.dhcp_trimIPHistory(cfg.get('CNR', 'keep_history_days')) logger.debug('dhcp trimIPhistory') nrcmd.dhcp_reload() logger.debug('dhcp reload') finally: unlock(cfg.getpath('App', 'lockfile')) except: e = sys.exc_info()[1] sendmail(cfg.get('SMTP', 'smtp'), cfg.get('SMTP', 'from'), cfg.get('SMTP', 'to'), 'CNR iphistory importer errors', str(e)) raise logger.debug('DONE')
def main(argv): hdb = HistDB() adb = AggDB() cfg = ConfigParser() cfg.read(hconfig_filename) hdb.connect(cfg.get("MySQL", "host"), cfg.get("MySQL", "user"), cfg.get("MySQL", "pass"), cfg.get("MySQL", "db")) cfg.read(aconfig_filename) adb.connect(cfg.get("MySQL", "host"), cfg.get("MySQL", "user"), cfg.get("MySQL", "pass"), cfg.get("MySQL", "db")) logging.config.fileConfig(config_logging_filename) logger = logging.getLogger("file") # All history: for item in hdb.aggregate_history(): adb.addlease(item) if item.complete: hdb.del_history_records(item.rows) logger.debug(str(item.rows) + " IDs deleted from history") nr = adb.cleanup(cfg.get("Maintenance", "record_keep_days")) logger.info(str(nr) + " leases deleted from aggregated")