Compare commits

27 Commits

Author SHA1 Message Date
2f8163f0f5 Failed arg 2024-05-05 23:47:56 +02:00
d4e7d822a9 Failed arg 2024-05-05 23:43:39 +02:00
3303730513 Failed arg 2024-05-05 23:40:45 +02:00
a490365265 Deleted prints 2024-05-05 21:54:06 +02:00
92364e261a Send message when host is available 2024-05-05 21:36:42 +02:00
65ba36b439 Merge pull request 'statusFeature' (#2) from statusFeature into main
Reviewed-on: #2
2024-05-05 21:29:48 +02:00
9355d9b2de Test Jenkinsfile
All checks were successful
Gitea/Status/pipeline/head This commit looks good
2024-05-05 19:58:01 +02:00
dc9c7eabc6 Merge branch 'main' of ssh://gitssh.mainserverprivate.org:8222/vgallegoiz/Status 2024-05-05 19:51:48 +02:00
e976c48103 Test Jenkinsfile 2024-05-05 19:51:29 +02:00
victor
ff9edd855e Added feature to save the state of the host 2024-05-05 00:26:48 +02:00
victor
76069b8193 Deleted Windows Lib 2024-05-04 22:30:35 +02:00
victor
aad6fb97a6 Añadido sistema de logs 2024-04-14 14:21:59 +02:00
victor
6ab6224ff6 deleted prints 2024-04-14 13:57:07 +02:00
victor
2b986eeabb dotenv no fuciona en RaspberryPi1 2024-04-14 13:50:22 +02:00
victor
90a7cb8581 dotenv no fuciona en RaspberryPi1 2024-04-14 13:48:26 +02:00
victor
7bccab9455 dotenv no fuciona en RaspberryPi1 2024-04-14 13:47:50 +02:00
03b7177ecf Merge pull request 'ReadIPs' (#1) from ReadIPs into main
Reviewed-on: #1
2024-04-14 13:29:40 +02:00
victor
807517be96 Added send_discord_message() 2024-04-14 13:24:09 +02:00
166067972e Changed main to define the structure of hosts and update 2024-04-08 23:52:03 +02:00
1aad27b937 Created toIP function and modified check_ping to work with ipaddress 2024-04-08 23:32:57 +02:00
2521b34df0 read_document function modified to just read the file and delete '\n' 2024-04-08 23:15:39 +02:00
69dd4bd1f4 Read IP document function created 2024-04-08 23:05:55 +02:00
87a3ee2886 Added function to ping a host 2024-04-07 14:24:43 +02:00
439a7d545e Added function to ping a host 2024-04-07 14:20:51 +02:00
victor
48f773f374 Added Try/except 2024-04-06 20:12:08 +02:00
victor
03dd3ba495 Added Try/except 2024-04-06 20:09:52 +02:00
victor
fc40664ddf Added Try/except 2024-04-06 20:07:21 +02:00
4 changed files with 90 additions and 9 deletions

1
.env Normal file
View File

@@ -0,0 +1 @@
DISCORD_WEBHOOK_URL='https://discord.com/api/webhooks/1229025346385608824/kIt2dp2znu0jR85QbOPmoMAteHaB9BCJDTMfn0cHE9mwpxHkbUNc9By2Y7n6gpufoyco'

10
Jenkinsfile vendored Normal file
View File

@@ -0,0 +1,10 @@
pipeline {
agent any
stages {
stage("Hello") {
steps {
echo "Hello world"
}
}
}
}

5
pyvenv.cfg Normal file
View File

@@ -0,0 +1,5 @@
home = C:\Users\vgall\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0
include-system-site-packages = false
version = 3.11.9
executable = C:\Users\vgall\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe
command = C:\Users\vgall\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe -m venv D:\Documentos\Status

View File

@@ -1,22 +1,87 @@
import argparse
from pythonping import ping
import ipaddress
import requests
import logging
DISCORD_WEBHOOK_URL='https://discord.com/api/webhooks/1229025346385608824/kIt2dp2znu0jR85QbOPmoMAteHaB9BCJDTMfn0cHE9mwpxHkbUNc9By2Y7n6gpufoyco'
logging.basicConfig(filename='/var/log/status.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def read_document(document_name):
with open(document_name, 'r') as file:
# Leer todas las líneas del archivo y guardarlas en un conjunto
return {linea.rstrip('\n') for linea in file}
def parseArguments():
# Crear un objeto ArgumentParser
parser = argparse.ArgumentParser(description='Ejemplo de script con flags')
# Agregar los argumentos con sus respectivos flags
parser.add_argument('-w', '--web', action='store', dest='web', help='Indicar una web')
parser.add_argument('-h', '--host', action='store', dest='host', help='Indicar un host')
parser.add_argument('-F', '--fail', action='store', dest='failed', help='Indicar un archivo con lista de Fails')
parser.add_argument('-H', '--host', action='store', dest='host', help='Indicar un archivo con lista de hosts')
# Parsear los argumentos de la línea de comandos
args = parser.parse_args()
try:
# Parsear los argumentos de la línea de comandos
return parser.parse_args()
except argparse.ArgumentError as e:
# Si ocurre un error al analizar los argumentos, mostrar un mensaje de error
#print("Error:", e)
# Mostrar el mensaje de ayuda
parser.print_help()
exit()
return args
def toIP(hosts):
ips = set()
for host in hosts:
try:
ip = ipaddress.IPv4Address(host)
ips.add(ip)
except ipaddress.AddressValueError:
logging.info(f"La entrada '{host}' no es una dirección IP válida.")
return ips
def check_ping(hosts, fails):
for host in hosts:
try:
response = ping(str(host), count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping
if not response.success():
logging.info(f"Bad ping: {host}")
if (host not in fails):
send_discord_message(f"Ping fallido para el host: {host}")
# Añadir el host a fails
fails.add(host)
elif response.success():
if host in fails:
fails.remove(host)
send_discord_message(f"{host} disponible de nuevo")
logging.info(f"Ping correct: {host}")
except Exception as e:
logging.info(f"Bad ping: {host}")
save_status(fails)
def save_status(fails):
with open(args.failed, "w") as file:
for fail in fails:
file.write(str(fail) + "\n")
file.close
def send_discord_message(message):
data = {
"content": message
}
try:
response = requests.post(DISCORD_WEBHOOK_URL, json=data)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logging.info(f"Error sending Discord message: {e}")
else:
logging.info("Discord message sent successfully!")
if __name__ == '__main__':
args = parseArguments()
if args.web:
print()# Parte web, comprobar que Código devuelve
else:
print()# Parte host, hará ping
ips_and_hosts = set()
fails = set()
if args.host:
ips_and_hosts.update(toIP(read_document(args.host)))
fails.update(toIP(read_document(args.failed)))
check_ping(ips_and_hosts, fails)