Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have a problem with my code and here is my code: log _ analyzer.py import re from datetime import datetime import csv import pytz
I have a problem with my code and here is my code:
loganalyzer.py
import re
from datetime import datetime
import csv
import pytz
class LogEntry:
def initself eventtime, internalip portnumber, protocol, action, ruleid sourceip country, countryname:
self.eventtime datetime.strptimeeventtime, Ymd H:M:S ZreplacetzinfopytzUTC
self.internalip internalip
self.portnumber portnumber
self.protocol protocol
self.action action
self.ruleid ruleid
self.sourceip sourceip
self.country country
self.countryname countryname
@property
def ipvclassself:
firstoctet intselfsourceipsplit
if firstoctet :
return A
elif firstoctet :
return B
elif firstoctet :
return C
elif firstoctet :
return D
else:
return 'Unknown'
@staticmethod
def countrycountlogentries, countrycode:
filteredenteries entry for entry in logentries if entry.country.lower countrycode.lower
printflenfilteredenteries log entries from the countrycode.upper were recorded."
return filteredenteries
@staticmethod
def parselogentries, month:
filteredentries entry for entry in logentries if entry.eventtime.month month
printflenfilteredentries log entries were recorded in month month
return filteredentries
index.py
from loganalyzer import LogEntry
import argparse
import csv
import pytz
def parseargs:
parser argparse.ArgumentParserdescription"Accept a CSV file of firewall log data and prepare it for analysis"
parser.addargumentfilename", f requiredTrue, help"Filename"
parser.addargumentaction", a requiredTrue, help"Execute an action on the CSV file valid values are 'head', 'deny', 'source', and 'parse'
parser.addargumentcountrycode", c helpletter country code for 'source' action"
parser.addargumentmonth", m typeint, help"Month for the 'parse' action"
return parser.parseargs
def printheadlogentries:
for logentry in logentries::
printlogentry.dict
def denycountlogentries:
deniedentries entry for entry in logentries if entry.action "Deny"
printfNumber of denied entries: lendeniedentries
def main:
args parseargs
filename args.filename
logentries
filename "firewalllogssample.csv
with openfilenamer encoding"utf as csvfile:
reader csvDictReadercsvfile
for row in reader:
logentry LogEntryroweventtime' rowinternalip rowportnumber' rowprotocol rowaction rowruleid rowsourceip rowcountry rowcountryname'
logentries.appendlogentry
if args.action "head":
printheadlogentries
elif args.action "deny":
denycountlogentries
elif args.action 'source' and args.country:
countrycount LogEntry.countrycountlogentries, args.country
elif args.action "parse":
if args.month and args.month :
filteredentries LogEntry.parselogentries, args.month
# Export filtered entries to CSV file
if filteredentries:
outputfilename fargsfilename.splitargsmonthlogscsv
with openoutputfilename, modew newline as csvfile:
fieldnames logentriesdictkeys
writer csvDictWritercsvfile fieldnamesfieldnames
writer.writeheader
for entry in filteredentries:
writer.writerowentrydict
printfFiltered log entries exported to outputfilename
else:
printInvalid month provided. Month must be an integer between and
if namemain:
main
this is the error i got from the output:
C:UsersakinnOneDrive Indiana University CITFirewall Log Analyzer vpython index.py filename firewalllogscsv action head
eventtime': datetime.datetime tzinfo 'internalip: 'portnumber': 'protocol': FTP Data', 'action': 'Allow', 'ruleid: 'sourceip: 'country': US 'countryname': 'United States'
C:UsersakinnOneDrive Indiana University CITFirewall Log Analyzer vpython index.py filename firewalllogscsv action deny
Number of denied entries:
C:UsersakinnOneDrive Indiana University CITFi
And this is what the output is supposed to look like:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started