Created
September 4, 2014 22:11
-
-
Save nerd0/52b2481026ef42dc5e82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Write a program which prints out all numbers between 1 and 100. When the program would print out a number exactly divisible by 4, #print "Linked" instead. When it would print out a number exactly divisible by 6, print "In" instead. When it would print out a #number exactly divisible by both 4 and 6, print "LinkedIn." | |
#!/usr/bin/python | |
for num in xrange(1,101): | |
#Cycle through numbers within 1-100 | |
if ( num % 6 == 0 and num % 4 == 0): | |
print "LinkedIn" | |
elif (num % 6 == 0): | |
print "In" | |
elif (num % 4 == 0): | |
print "Linked" | |
else: | |
print num | |
================================================== | |
---------- begin sample log extract ---------- | |
Jan 20 03:25:08 fakehost logrotate: ALERT exited abnormally with [1] | |
Jan 20 03:25:08 fakehost run-parts(/etc/cron.daily)[20447]: finished logrotate | |
Jan 20 03:26:21 fakehost anacron[28969]: Job 'cron.daily' terminated | |
Jan 20 03:26:21 fakehost anacron[28969]: Normal exit (1 job run) | |
Jan 20 03:30:01 fakehost CROND[31462]: (root) CMD (/usr/lib64/sa/sa1 1 1) | |
Jan 20 03:30:01 fakehost CROND[31461]: (root) CMD (/var/system/bin/sys-cmd -F > /dev/null 2>&1) | |
Jan 20 05:03:03 fakehost ntpd[3705]: synchronized to time.faux.biz, stratum 2 | |
Jan 20 05:20:01 fakehost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="20438" x-info="http://www.rsyslog.com"] start | |
Jan 20 05:22:04 fakehost cs3[31163]: Q: ".../bin/rsync -LD ": symlink has no referent: "/var/syscmds/fakehost/runit_scripts/etc/runit/service/superImportantService/run"#012Q: ".../bin/rsync -LD ": rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6] | |
Jan 20 05:22:04 fakehost cs3[31163]: I: Last 2 quoted lines were generated by "/usr/local/bin/rsync -LD --recursive --delete --password-file=/var/syscmds/modules/rsync_password /var/syscmds/fakehost syscmds@fakehost::syscmds_rsync" | |
Jan 20 05:22:08 fakehost cs3[31163]: Q: ".../sbin/sv restart": ok: run: /export/service/cool-service: (pid 32323) 0s | |
Jan 20 05:22:08 fakehost cs3[31163]: I: Last 1 quoted lines were generated by "/sbin/sv restart /export/service/cool-service" | |
Jan 20 05:22:09 fakehost cs3[31163]: R: cs3: The cool service on fakehost does not appear to be communicating with the cool service leader. Automating a restart of the cool service in attempt to resolve the communication problem. | |
Jan 20 05:22:37 fakehost ACCT_ADD: WARNING: Manifest /var/syscmds/inputs/config-general/doit.txt has been processed already, bailing | |
---------- end sample log extract ---------- | |
Write a script which parses /var/log/messages and generates a CSV with two columns: minute, number_of_messages | |
---------- begin sample output ---------- | |
minute, number_of_messages | |
Jan 20 03:25,2 | |
Jan 20 03:26,2 | |
Jan 20 03:30,2 | |
Jan 20 05:03,1 | |
Jan 20 05:20,1 | |
Jan 20 05:22,6 | |
---------- end sample output ---------- | |
#!/usr/bin/python | |
import re | |
output = {} | |
#Parse out the timedate stamp Jan 20 05:22:37 to capture two groups 1.) Jan 20 05:22 2.) log output e.g. | |
#R: cs3: The cool service on fakehost does not appear to be communicating with the cool | |
regex = re.compile(r'^(\w+ \d+ \d+:\d+):\d+ \w+ .*$') | |
with open("/var/log/messages", "r+") as myfile: | |
for log_line in myfile: | |
match = regex.match(log_line) | |
if match: | |
if match.group(0): | |
try: | |
output[match.group(0)] += 1 | |
except Exception as e: | |
output[match.group(0)] = 0 | |
else: | |
print "Parsing line partically succeeded: %s " % (log_line) | |
else: | |
print "ERROR: Malformed logline %s" % (log_line) | |
output_file = open("/tmp/counts", "w") | |
for (dt,count) in output.items(): | |
output_file.write("%s,%s" % (dt,count)) | |
============================================= | |
Extract the program name from the field between the hostname and the log message and output those values in columns. | |
Sample Output (when run against the lines containing "Jan 20 05:2" in the log above): | |
---------- begin sample output ---------- | |
minute,total_count,rsyslogd,cs3,ACCT_ADD | |
Jan 20 05:20,1,1,0,0 | |
Jan 20 05:22,6,0,5,1 | |
---------- end sample output ---------- | |
#!/usr/bin/python | |
import re | |
output = {} | |
#Parse out the timedate stamp Jan 20 05:22:37 to capture two groups 1.) Jan 20 05:22 2.) log output e.g. | |
#R: cs3: The cool service on fakehost does not appear to be communicating with the cool | |
regex = re.compile(r'^(\w+ \d+ \d+:\d+):\d+ \w+ (\w+).*$') | |
with open("/var/log/messages", "r+") as myfile: | |
for log_line in myfile: | |
match = regex.match(log_line) | |
if match: | |
if match.group(1) and match.group(2): | |
try: | |
output[match.group(1)]['total_count'] += 1 | |
output[match.group(1)][match.group(2)] += 1 | |
except KeyError: | |
output[match.group(1)] = {'total_count':0} | |
else: | |
print "Parsing line partically succeeded: %s " % (log_line) | |
else: | |
print "ERROR: Malformed logline %s" % (log_line) | |
output_file = open("/tmp/counts", "w") | |
for (dt,count) in output.items(): | |
output_file.write("%s,%s" % (dt,count)) | |
================================================= | |
Assume there is a REST API available at "http://www.linkedin.corp/api" for accessing employee information The employee information endpoint is "/employee/<id>" Each employee record you retrieve will be a JSON object with the following keys: | |
'name' refers to a String that contains the employee's first and last name | |
'title' refers to a String that contains the employee's job title | |
'reports' refers to an Array of Strings containing the IDs of the employee''s direct reports | |
Write a function that will take an employee ID and print out the hierarchy of employees under that employee. | |
-----------Begin Sample Output-------------- | |
Flynn Mackie - Senior VP of Engineering | |
Wesley Thomas - VP of Design | |
Randall Cosmo - Director of Design | |
Brenda Plager - Senior Designer | |
Nina Chiswick - VP of Engineering | |
Tommy Quinn - Director of Engineering | |
Jake Farmer - Frontend Manager | |
Liam Freeman - Junior Code Monkey | |
Sheila Dunbar - Backend Manager | |
Peter Young - Senior Code Cowboy | |
-----------End Sample Output-------------- | |
import requests | |
import json | |
report = [] | |
def print_hierachy(employee_id): | |
employee = requests.get('http://www.linkedin.corp/api/employee/%s' % employee_id) | |
if employee.status == 200: | |
print employee['name'], employee['title'] | |
result = json.reads(employee.json) | |
for report in result['reports']: | |
print_hierachy(report) | |
return 0 | |
##to get same output as above
#-------------------------------
minute, number_of_messages
Jan 20 03:25,2
Jan 20 03:26,2
Jan 20 03:30,2
Jan 20 05:03,1
Jan 20 05:20,1
Jan 20 05:22,6
-------------------------------#
#!/usr/bin/python
import re
#Parse out the timedate stamp
l =[]
regex = re.compile(r'^(\w+ \d+ \d+:\d+):\d+ \w+ .*$')
with open("/var/log/messages", "r+") as myfile:
for log_line in myfile:
match = regex.match(log_line)
if match:
l.append(match.group(1))
output = dict((i, l.count(i)) for i in l)
print output
print "minute, number_of_messages"
for k,v in sorted(output.items()):
print '{0},{1}'.format(k,v)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write a script which parses /var/log/messages and generates a CSV with two columns: minute, number_of_messages
#!/usr/bin/python
import re
l =[]
#Parse out the timedate stamp
regex = re.compile(r'^(\w+ \d+ \d+:\d+):\d+ \w+ .*$')
with open("/var/log/messages", "r+") as myfile:
for log_line in myfile:
match = regex.match(log_line)
if match:
l.append(match.group(1))
output = dict((i, l.count(i)) for i in l)
for k,v in sorted(output.items()):
print k,v