mirror of https://github.com/r4sas/py-i2phosts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.7 KiB
91 lines
2.7 KiB
14 years ago
|
#!/usr/bin/python
|
||
|
|
||
|
import os
|
||
|
import sys
|
||
|
import argparse
|
||
|
import datetime
|
||
14 years ago
|
import configobj
|
||
14 years ago
|
|
||
|
from i2p import samclasses
|
||
|
from i2p import socket
|
||
|
|
||
|
# parse command line options
|
||
|
parser = argparse.ArgumentParser(
|
||
|
description='Hosts checker for py-i2phosts.',
|
||
|
epilog='Report bugs to http://zzz.i2p/topics/733')
|
||
|
parser.add_argument('-d', '--debug', action='store_true',
|
||
14 years ago
|
help='set loglevel to debug and write messages to stdout'),
|
||
14 years ago
|
parser.add_argument('-v', '--verbose', action='store_true',
|
||
|
help='set loglevel to info and write messages to stdout'),
|
||
14 years ago
|
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/checker.conf', dest='config_file',
|
||
|
help='config file to use')
|
||
14 years ago
|
args = parser.parse_args()
|
||
|
|
||
14 years ago
|
# read config
|
||
14 years ago
|
spec = '''
|
||
|
log_file = string(default='/var/log/py-i2phosts/master.log')
|
||
|
log_level = option('debug', 'info', 'warning', 'error', 'critical', default='info')
|
||
|
'''
|
||
|
spec = spec.split('\n')
|
||
|
config = configobj.ConfigObj(args.config_file, configspec=spec)
|
||
14 years ago
|
if 'include' in config:
|
||
|
config_included = configobj.ConfigObj(config['include'])
|
||
|
config.merge(config_included)
|
||
14 years ago
|
|
||
14 years ago
|
# django setup
|
||
|
DJANGO_SETTINGS_MODULE = 'settings'
|
||
14 years ago
|
if 'DJANGO_PROJECT_PATH' in config:
|
||
14 years ago
|
DJANGO_PROJECT_PATH = config['DJANGO_PROJECT_PATH']
|
||
|
else:
|
||
|
DJANGO_PROJECT_PATH = os.path.dirname(sys.argv[0]) + '/web'
|
||
|
sys.path.insert(1, DJANGO_PROJECT_PATH)
|
||
|
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
|
||
14 years ago
|
from pyi2phosts.postkey.models import i2phost
|
||
|
from pyi2phosts.lib.utils import get_logger
|
||
|
from pyi2phosts.lib.utils import validate_config
|
||
14 years ago
|
from pyi2phosts.lib.utils import get_b32
|
||
14 years ago
|
|
||
|
# validate config
|
||
|
validate_config(config)
|
||
14 years ago
|
|
||
14 years ago
|
# configure logger
|
||
14 years ago
|
if args.debug == True:
|
||
|
log_level = 'debug'
|
||
|
log_file = None
|
||
14 years ago
|
elif args.verbose == True:
|
||
|
log_level = 'info'
|
||
|
log_file = None
|
||
14 years ago
|
else:
|
||
14 years ago
|
log_level = config['log_level']
|
||
|
log_file = config['log_file']
|
||
14 years ago
|
log = get_logger(filename=log_file, log_level=log_level)
|
||
14 years ago
|
|
||
|
# determine SAM interface address
|
||
|
if 'sam_addr' in config:
|
||
|
sam_addr = config['sam_addr']
|
||
|
else:
|
||
|
log.warning('SAM address isn\'t specified in config, falling back to localhost')
|
||
|
sam_addr = '127.0.0.1:7656'
|
||
|
S = samclasses.BaseSession(sam_addr)
|
||
|
|
||
14 years ago
|
all_hosts = i2phost.objects.all().order_by('-activated', '-last_seen')
|
||
14 years ago
|
log.info('starting check')
|
||
|
for host in all_hosts:
|
||
14 years ago
|
log.debug('%s: testing...', host.name)
|
||
14 years ago
|
# get b32 address from full dest key
|
||
|
dest = host.b64hash
|
||
14 years ago
|
b32dest = get_b32(dest)
|
||
14 years ago
|
# do name lookup query with b32 address
|
||
|
# it success only if host is alive
|
||
|
try:
|
||
|
a = S._namelookup(b32dest)
|
||
|
except socket.NetworkError, e:
|
||
|
log.debug('%s: %s', host.name, e.args[0][0])
|
||
|
continue
|
||
|
log.info('alive host: %s', host.name)
|
||
|
# update lastseen timestamp
|
||
14 years ago
|
host.last_seen = datetime.datetime.utcnow()
|
||
14 years ago
|
host.save()
|
||
|
S.close()
|
||
|
log.info('check finished')
|