131 lines
2.9 KiB
Python
131 lines
2.9 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# sendReboot v. 1.8
|
|
|
|
#---------------------------------------------------------------
|
|
|
|
|
|
import time
|
|
import sys
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import urllib
|
|
import ConfigParser
|
|
import os, sys
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
#---------------------------------------------------------------
|
|
|
|
# COSTANTI
|
|
PROGRAM_NAME = "SendReboot IOB-pi v.1.8"
|
|
|
|
# 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.urlopen ( url )
|
|
numTry = numTry + 10
|
|
except Exception, e:
|
|
print e
|
|
logging.info ( e )
|
|
print "Url aforte" , 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.read ( 'IOB.cfg' )
|
|
|
|
idxMacchina = config.get ( 'id' , 'idxMacchina' )
|
|
|
|
|
|
URLREBO = config.get ( 'web' , 'URLREBO' )
|
|
|
|
LOGFILE = config.get ( 'log' , 'LOGREBO' )
|
|
except:
|
|
print "\n\n" + PROGRAM_NAME + ' - Error 4 - in config file ' 'IOB.cfg'
|
|
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:
|
|
# manda mail o simili - FARE!!!
|
|
print "LOG: Impossibile creare file log con nome "
|
|
print (LOGFILE)
|
|
#--------------------------------------------
|
|
|
|
|
|
print "\n\n" + PROGRAM_NAME + "\n\n"
|
|
|
|
global startstatus
|
|
startstatus = 1
|
|
|
|
if startstatus == 1:
|
|
logging.info("Avvio Programma " + PROGRAM_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 )
|