Files
2025-07-31 14:02:30 +02:00

139 lines
3.3 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# sendReboot v. 1.8
# - (2.5.2) Fix gestione eccezioni con report dettagliato
#---------------------------------------------------------------
import time
import sys
from datetime import datetime
#import urllib
#import ConfigParser
import urllib.request
import configparser
import os, sys
import logging
import time
#---------------------------------------------------------------
# COSTANTI
SR_PROG_NAME = "SendReboot IOB-pi v.2.5.2"
# DA FILE CONF
idxMacchina = "99"
# registro se ho fatto chiamata
global numTry
numTry = 1
#---------------------------------------------------------------
#Funzione di scrittura su url con try-except
#---------------------------------------------------------------
def chiamaUrl(numTry):
try:
urllib.request.urlopen ( url )
numTry = numTry + 10
except Exception as e:
print("Errore in chiamaUrl")
print(str(e))
logging.info ( str(e) )
print("Url chiamato: " , url)
return numTry
#---------------------------------------------------------------
# Funzione di recupero mac address per poterlo inviare a MPIO
#---------------------------------------------------------------
def getMAC(interface):
# Return the MAC address of interface
try:
str = open('/sys/class/net/' + interface + '/address').read()
except:
str = "00:00:00:00:00:00"
return str[0:17]
#---------------------------------------------------------------
# MAIN
#---------------------------------------------------------------
try:
#config = ConfigParser.RawConfigParser()
config = configparser.RawConfigParser()
config.read ( 'IOB.cfg' )
idxMacchina = config.get ( 'id' , 'idxMacchina' )
URLREBO = config.get ( 'web' , 'URLREBO' )
LOGFILE = config.get ( 'log' , 'LOGREBO' )
except Exception as e:
print("\n\n" + SR_PROG_NAME + ' - Error 4 - in config file ' 'IOB.cfg')
print(str(e))
sys.exit(1)
#--------------------------------------------
# oggetto Logger
#--------------------------------------------
try:
# log = Logger(LOGFILE)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-8s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=LOGFILE,
filemode='a')
except Exception as e:
# manda mail o simili - FARE!!!
print("LOG: Impossibile creare file log con nome ")
print(LOGFILE)
print(str(e))
#--------------------------------------------
print("\n\n" + SR_PROG_NAME + "\n\n")
global startstatus
startstatus = 1
if startstatus == 1:
logging.info("Avvio Programma " + SR_PROG_NAME)
# lettura file configurazione
print ( ' idxMacchina = %s' % ( idxMacchina ) )
print ( ' URLREBO = %s' % ( URLREBO ) )
print ( ' LOGFILE = %s' % ( LOGFILE ) )
# lettura mac address
myMac = getMAC('eth0')
print ( ' MAC Address = %s' % ( myMac ) )
# configuro URL da inviare
url = URLREBO + idxMacchina + "&mac=" + myMac
# modifica: cerco se ho inviato segnale altrimenti ritento invio...
while (numTry < 11):
logging.info("Tentativo invio URL: $numTry" )
numTry = chiamaUrl(numTry)
time.sleep(3)
numTry = numTry + 1
# registro che ho inviato!
logging.info("Inviato segnale di reboot!: " + url )