def main():
exit_status = 1
try:
args = parse_args()
# Make sure the exporter is only running once.
lock_file = '/var/lock/{}.lock'.format(os.path.basename(sys.argv[0]))
lock_fd = os.open(lock_file, os.O_CREAT)
lock_success = False
try:
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
lock_success = True
except IOError:
msg = 'Failed to export metrics, another instance is running.'
syslog.syslog(syslog.LOG_INFO, msg)
sys.stderr.write(msg + '\n')
if lock_success:
# Create a new registry, otherwise unwanted default collectors are
# added automatically.
registry = prometheus_client.CollectorRegistry()
# Register our own collector and write metrics to STDOUT.
registry.register(CephRgwCollector(**vars(args)))
sys.stdout.write(prometheus_client.generate_latest(registry))
sys.stdout.flush()
# Unlock the lock file.
fcntl.flock(lock_fd, fcntl.LOCK_UN)
exit_status = 0
except Exception as e:
syslog.syslog(syslog.LOG_ERR, str(e))
# Cleanup
os.close(lock_fd)
if lock_success:
try:
os.unlink(lock_file)
except:
pass
sys.exit(exit_status)
评论列表
文章目录