add Telegram messages
pull/19/head
Aidaho12 7 years ago
parent 18c9f6b9c2
commit bc9bb95b28

@ -20,6 +20,7 @@ For install just dowload archive and untar somewhere:
$ cd /opt $ cd /opt
$ unzip master.zip $ unzip master.zip
$ mv haproxy-web-interface-master/ haproxy $ mv haproxy-web-interface-master/ haproxy
$ cd /opt/haproxy
$ pip3 install -r requirements.txt $ pip3 install -r requirements.txt
$ chmod +x server.py $ chmod +x server.py
$ chmod +x -R cgi-bin/ $ chmod +x -R cgi-bin/

@ -44,6 +44,19 @@ def logging(serv, action):
log.write(mess) log.write(mess)
log.close log.close
if config.get('telegram', 'enable') == "1": telegram_send_mess(mess)
def telegram_send_mess(mess):
import telegram
token_bot = config.get('telegram', 'token')
channel_name = config.get('telegram', 'channel_name')
proxy = config.get('telegram', 'proxy')
if proxy is not None:
pp = telegram.utils.request.Request(proxy_url=proxy)
bot = telegram.Bot(token=token_bot, request=pp)
bot.send_message(chat_id=channel_name, text=mess)
def check_login(ref): def check_login(ref):
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
login = cookie.get('login') login = cookie.get('login')
@ -111,15 +124,22 @@ def footer():
'<div class="footer-link">') '<div class="footer-link">')
links() links()
print('</div></div></body></html>') print('</div></div></body></html>')
def get_config(serv, cfg): def ssh_connect(serv):
os.chdir(hap_configs_dir)
ssh = SSHClient() ssh = SSHClient()
ssh.load_system_host_keys() ssh.load_system_host_keys()
k = paramiko.RSAKey.from_private_key_file(ssh_keys)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = serv, username = ssh_user_name, pkey = k )
if config.get('ssh', 'ssh_keys_enable') == "1":
k = paramiko.RSAKey.from_private_key_file(ssh_keys)
ssh.connect(hostname = serv, username = ssh_user_name, pkey = k )
else:
ssh.connect(hostname = serv, username = ssh_user_name, password = config.get('ssh', 'ssh_pass'))
return ssh
def get_config(serv, cfg):
os.chdir(hap_configs_dir)
ssh = ssh_connect(serv)
sftp = ssh.open_sftp() sftp = ssh.open_sftp()
sftp.get(haproxy_config_path, cfg) sftp.get(haproxy_config_path, cfg)
sftp.close() sftp.close()
@ -167,13 +187,7 @@ def upload_and_restart(serv, cfg):
now_utc = datetime.now(timezone(config.get('main', 'time_zone'))) now_utc = datetime.now(timezone(config.get('main', 'time_zone')))
tmp_file = tmp_config_path + "/" + now_utc.strftime(fmt) + ".cfg" tmp_file = tmp_config_path + "/" + now_utc.strftime(fmt) + ".cfg"
ssh = SSHClient() ssh = ssh_connect(serv)
ssh.load_system_host_keys()
k = paramiko.RSAKey.from_private_key_file(ssh_keys)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("connecting<br />")
ssh.connect( hostname = serv, username = ssh_user_name, pkey = k )
print("connected<br />") print("connected<br />")
sftp = ssh.open_sftp() sftp = ssh.open_sftp()
sftp.put(cfg, tmp_file) sftp.put(cfg, tmp_file)
@ -250,12 +264,7 @@ def show_ip(stdout):
print(line) print(line)
def ssh_command(serv, commands, **kwargs): def ssh_command(serv, commands, **kwargs):
ssh = SSHClient() ssh = ssh_connect(serv)
ssh.load_system_host_keys()
k = paramiko.RSAKey.from_private_key_file(ssh_keys)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect( hostname = serv, username = ssh_user_name, pkey = k )
ip = 0 ip = 0
compare_funct = 0 compare_funct = 0

@ -3,7 +3,6 @@
fullpath = /opt/haproxy/ fullpath = /opt/haproxy/
server_bind_ip = 0.0.0.0 server_bind_ip = 0.0.0.0
server_port = 8000 server_port = 8000
#Log for server.py
log_path = %(fullpath)s/log/ log_path = %(fullpath)s/log/
time_zone = UTC time_zone = UTC
#Enable logo on top menu. Default disable #Enable logo on top menu. Default disable
@ -17,16 +16,28 @@ haproxy_configs_server = localhost
haproxy_save_configs_dir = /opt/haproxy/cgi-bin/hap_config/ haproxy_save_configs_dir = /opt/haproxy/cgi-bin/hap_config/
[ssh] [ssh]
#If ssh connect disable entare password for ssh connect. Default enable
ssh_keys_enable = 1
#SSH keys to connect without password to HAproxy servers #SSH keys to connect without password to HAproxy servers
ssh_keys = /opt/haproxy/cgi-bin/id_rsa.pem ssh_keys = /opt/haproxy/cgi-bin/id_rsa.pem
#Username for connect ssh #Username for connect ssh
ssh_user_name = root ssh_user_name = root
ssh_pass =
[logs] [logs]
#Logs save localy, disable by default #Logs save locally, disable by default
local_path_logs = /var/log/haproxy.log local_path_logs = /var/log/haproxy.log
#If exist syslog server for HAproxy logs
syslog_server_enable = enable syslog_server_enable = enable
syslog_server = 172.28.5.112 syslog_server =
[telegram]
#Send log message to telegram channel
#Default bot send message disable
enable = 0
token =
channel_name =
proxy =
[haproxy] [haproxy]
#Command for restart HAproxy service #Command for restart HAproxy service
@ -36,10 +47,7 @@ user = admin
password = password password = password
stats_port = 8085 stats_port = 8085
haproxy_config_path = /etc/haproxy/haproxy.cfg haproxy_config_path = /etc/haproxy/haproxy.cfg
<<<<<<< HEAD
#Temp store configs, for haproxy check #Temp store configs, for haproxy check
tmp_config_path = /tmp tmp_config_path = /tmp
#Time in seconds for auto refresh view stats_port #Time in seconds for auto refresh view stats_port
refresh_time = 120 refresh_time = 120

Loading…
Cancel
Save