Skip to content

Instantly share code, notes, and snippets.

@ljmocic
Created November 12, 2018 21:32
Show Gist options
  • Save ljmocic/67d22cb095d98ae49f0484bd1129a415 to your computer and use it in GitHub Desktop.
Save ljmocic/67d22cb095d98ae49f0484bd1129a415 to your computer and use it in GitHub Desktop.
import os
import requests
import psutil
import time
import json
# Here you can find basic configuration settings set through variables
IP_TO_PING = '192.168.1.1' # IP of your computer on the network for ping
API_ENDPOINT = 'https://www.yoururl.com/' # API endpoint which needs to be notified if everything goes as planned
DATA_FILE = 'data.json' # Since we use the file for saving the counter, here is the name of the file where it will be saved
# This is how the functions are written in Python, they start with def block
# Be sure to give attention to indentation. More info: http://www.peachpit.com/articles/article.aspx?p=1312792&seqNum=3
def call_the_api():
print('[INFO] Notifying the API: ' + API_ENDPOINT)
# Here we use library for making HTTP request over the network
# By using method `get` we make a GET HTTP method to given link
#requests.get('API_ENDPOINT') # Uncomment this when you enter API_ENDPOINT for notification
# Uses os package to use system module which can pass command through terminal
def ping():
response = os.system("ping -c 1 " + IP_TO_PING)
# Since in UNIX operating system, the sign of running without problems is 0
# We test if the response of the system to this command was positive
if response == 0:
print('[INFO] Successfull ping: ' + IP_TO_PING)
return True
else:
return False
# Psutil library have been used to get boot time
# Time module for getting current time
# Next step is just formatting the given data to minutes
def uptime_minutes():
# The whole method could be written as commented line below
# return (time.time() - psutil.boot_time()) / 60 / 60
current_time = time.time() - psutil.boot_time()
seconds = current_time / 60
minutes = seconds / seconds
print('[INFO] Machine has been active for ' + str(int(minutes)) + ' minutes')
return int(minutes)
# Persisting the counter to file in current folder by name of DATA_FILE variable
def save_counter(counter_value):
data_file = open(DATA_FILE, 'w') # Opening the file in write mode
data_json = {} # Initializing the json/dict
data_json['counter'] = counter_value # Saving the counter value
data_file.write(json.dumps(data_json)) # Writing 'dumped' json to file
data_file.close() # Closing the file
def load_counter():
if os.path.exists(DATA_FILE): # Checking if file exists if it does not this function will return 0
data_file = open(DATA_FILE, 'r') # Open file in read mode
json_data = json.load(data_file) # Load json from the file
data_file.close() # Since we have loaded json file into the variable we can close the file
return json_data['counter'] # return counter data from the file
else:
return 0
def main():
counter = load_counter()
if uptime_minutes() > 60:
if ping() == True:
if counter == 5:
call_the_api()
counter = 0
else:
counter += 1
else:
counter = 0
else:
counter = 0
save_counter(counter) # Persisting the counter
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment