Skip to content

Instantly share code, notes, and snippets.

@socalal
Last active April 18, 2016 22:20
Show Gist options
  • Save socalal/478c023ffa6b0302e771254f4ea20cc7 to your computer and use it in GitHub Desktop.
Save socalal/478c023ffa6b0302e771254f4ea20cc7 to your computer and use it in GitHub Desktop.
Fix App Type ID - socalal
#!/usr/bin/python
# Created by Alan M. Orther - alan.orther@ingenu.com
# This script is used to find the AMI 1.0 devices that are not switching from
# App Type 10 to App Type 30 when upgrading from AMI 1.0 to AMI 1.2.
# The script will create a directory called /aotools in the user's home
# directory and read the current bus log for specific errors. The script will
# then copy the grepped log to the /aotools directory and show a list of the
# nodes that have the error in decimal and hex form. It will also provide a
# count of nodes and duplicates. I will generally use this script around 45
# minutes into the hour to provide time to make the DB changes before the
# next hour of logs rolls over.
import os
import time
# Script global counter variables.
total_count = 0
duplicate_count = 0
uniq_count = 0
nodes_converted_count = 0
# Create aotools directory in user's home directory if it doesn't already exist.
aotools_dir = "~/aotools/"
aotools_dir = os.path.expanduser(aotools_dir)
if not os.path.exists(aotools_dir):
os.makedirs(aotools_dir)
# Create function to use on the HES server.
def hes_function():
global total_count, duplicate_count, uniq_count, nodes_converted_count
datetime = str(time.strftime("%m%d%Y-%H%M%S"))
print '\nDatetime file timestamp: ' + datetime + '\n'
system_command = 'cat /opt/onramp_apps/hes/instance_1/logs/bus | grep ' \
'"sent frag" > ~/aotools/grepped_logs-%s.txt' % datetime
os.system(system_command)
# Open file with grepped information.
log_filename = 'grepped_logs-%s.txt' % datetime
greppedlogstxt = open(aotools_dir + log_filename)
# Make file's lines into a list
grepped_txt_lines = greppedlogstxt.readlines()
# Create empty list to put node IDs.
node_ids = []
# Split the lines up and place the node ID in the node_id list
for line in grepped_txt_lines:
before_keyword, keyword, after_keyword = line.partition('| Node id ')
nodeid, keyword, after_keyword = after_keyword.partition(' sent frag ')
node_ids.append(nodeid)
# Make lists and a set for comparison.
seen = set()
uniq = []
duplicates = []
nodes_converted = []
for x in node_ids:
total_count += 1
if x in seen:
duplicate_count += 1
duplicates.append(x)
if x not in seen:
uniq_count += 1
uniq.append(x)
seen.add(x)
# Print the values for nodes and counts.
print '[%s]' % ','.join(map(str, uniq))
print '\n'
for n in uniq:
try:
nodes_converted_count += 1
n = hex(int(n))
nodes_converted.append(n)
except ValueError:
print "\n --== ERROR ==-- ****** CONVERSION ERROR ******\n"
print '[%s]' % ','.join(map(str, nodes_converted))
print '\n'
print 'Unique Node IDs: %s \n' % uniq_count
print 'Unique MAC IDs: %s \n' % nodes_converted_count
print 'Duplicate Node IDs: %s \n' % duplicate_count
# Check to see if the script is on the hes server. Auto-start script.
if not os.path.exists("/opt/ulp/gateway/instances/default/logs/"):
hes_function()
else:
# Not used yet..... Stay tuned........
gateway_function()
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment