Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework for it-support-engineer Bruce Diao #1967

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions it-support-engineer/InterviewLog.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<#
将输入的原始日志时间格式转换为timeWindow格式,并返回一个timeWindow的字符串
#>
Function ConvertTo-TimeWindow
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, Position = 0)]$TimeString
)
[int]$Hour = ($TimeString -split":")[0]

switch ($Hour)
{
{$_ -lt 9} {[string]$StartTimeWindow = '0' + $Hour; [string]$EndTimeWindow = '0' + ($Hour + 1);[string]$TimeWindowString = $StartTimeWindow +"00" + "-" + $EndTimeWindow + "00";return $TimeWindowString}
{$_ -eq 9} {[string]$StartTimeWindow = '0' + $Hour; [string]$TimeWindowString = $StartTimeWindow +"00" + "-" + "1000";return $TimeWindowString}
{$_ -ge 10} {[string]$StartTimeWindow = $Hour;[string]$EndTimeWindow = $Hour + 1;[string]$TimeWindowString = $StartTimeWindow +"00"+ "-" + $EndTimeWindow + "00";return $TimeWindowString}
}

}

<#
将原始日志文件根据条件筛选后进行对应字符串处理,并格式化timeWindow日期,返回一个数组对象
#>
Function ConvertTo-LogFile
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, Position = 0)]$LogPath
)
$logs = Get-Content $LogPath
$Type = @()
$LogCount = $logs.Count
for ($i = 0; $i -lt $LogCount ; $i++)
{
switch ($logs[$i])
{
{($_ -match '.*\[\d.*\]') -and ($_ -notmatch 'Notice\:') -and ($_ -notmatch 'WARNING\:') -and ($_ -notmatch 'failed\:')} {
$Type += New-Object -TypeName Psobject -Property @{
"deviceName" = ($_ -split " ",6)[3]
"processId" = ($_ -split " ",6)[4].Split("[")[1].Split("]")[0]
"processName" = ($_ -split " ",6)[4].Split("[")[0]
"description"= ($_ -split " ",6)[5]
"timeWindow" = ConvertTo-TimeWindow -TimeString ($_ -split " ",4)[2].Split(":")[0]
}
}

{$_ -match 'Configuration Notice\:'} {
$Type += New-Object -TypeName Psobject -Property @{
"deviceName" = ($_ -split " ",6)[3]
"processId" = ($_ -split " ",6)[4].Split("[")[1].Split("]")[0]
"processName" = ($_ -split " ",6)[4].Split("[")[0]
"description"= ($_ -split ":",4)[3] + $logs[$i+1] + $logs[$i+2]
"timeWindow" = ConvertTo-TimeWindow -TimeString ($_ -split " ",4)[2].Split(":")[0]
}
}

{($_ -match 'WARNING\:') -or ($_ -match 'failed\:')} {
$Type += New-Object -TypeName Psobject -Property @{
"deviceName" = ($_ -split " ",6)[3]
"processId" = ($_ -split ":",8)[2].Split("[")[1].Split("]")[0]
"processName" = ($_ -split " ",5)[4].Split(":")[0].Split("[")[0]
"description"= ($_ -split ":",4)[3]
"timeWindow" = ConvertTo-TimeWindow -TimeString ($_ -split " ",4)[2].Split(":")[0]
}
}
}
}
return $Type
}

<#
输入原始日志文件及需要提交的网址,最终生成指定Key的Josn文件并上传到服务器�?
#>

Function Submit-PostRequest
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, Position = 0)]$FilePath,
[Parameter(Mandatory = $true, Position = 1)]$Url,
[switch]$LogCsv
)
# 将原始数据按条件进行字符串处理
$LogFile = ConvertTo-LogFile -LogPath $FilePath

# 将日志按小时级内发生的次数进行处理(去除重复日志信息)
$JosnReport = @()
$LogFile | Group-Object -Property timeWindow, deviceName, processId, processName, description | ForEach-Object{
$JosnReport += New-Object -TypeName PsObject -Property @{
"deviceName" = $_.Group[0].deviceName
"processId" = $_.Group[0].processId
"processName" = $_.Group[0].processName
"description" = $_.Group[0].description
"timeWindow" = $_.Group[0].timeWindow
"numberOfOccurrence" = $_.Count
}
}
# 将数组对象转换为Json格式
$SubmitJson = $JosnReport | Select-Object -Property deviceName,processId,processName,description,timeWindow,numberOfOccurrence | ConvertTo-Json

# 如果有LogCsv开关参数打开,输出CSV格式日志到本地
if ($LogCsv) {
$JosnReport | Export-Csv -Path .\Interview.csv -NoTypeInformation -Encoding UTF8
}

# Windows10、11 默认Powershell版本5.1,用于不受信任证书的运行环境

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Invoke-WebRequest -ContentType "application/json" -Method POST -Body $SubmitJson -Uri $url
}


<#
执行主函数,输入原始日志文件及需要提交的网址,需要本地输出可以添加“-Logcsv”开关参数
#>
Submit-PostRequest -FilePath "D:\interview_data_set" -Url "https://foo.com/bar"
85 changes: 85 additions & 0 deletions it-support-engineer/InterviewLog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python3
import re
import json
import requests

def main(logfile,url):
# 将原始日志进行字符串处理
json_dict=list_logs(logfile)
# 去除重复日志
new_list=group(json_dict)
# 统计日志发生次数
new_list=count(json_dict,new_list)
# Post Json文件到服务器
send_json(new_list,url)
# print(json_dict)

def list_logs(filepath):
json_dict=[]
with open(filepath) as file:
lines= file.readlines()
for text in lines:
if '---' in text:
text=pre_text
elif 'Notice:' in text:
t=0
pre_text=text
continue
elif ' ' in text and t!=1 :
pre_text=pre_text.split('\n')[0]+text.split(' ')[1]
t+=1
continue
elif ' ' in text and t==1:
text=pre_text+text
json_dict.append(analysis(text))
pre_text=text
return json_dict

def group(list):
new_list = []
for it in list:
if it not in new_list:
new_list.append(it)
return new_list

def count(list,new_list):
# print(new_list)
for it in new_list:
total=list.count(it)
it['numberOfOccurrence']=total
# if total>1:
# print(it)
return new_list

def send_json(list,url):
requests.packages.urllib3.disable_warnings()
headers={'Content-Type': 'application/json;charset=utf-8'}
try:
response = requests.post(url=url, headers=headers, data=json.dumps(list),verify=False)
if response.status_code == 200:
# return response.json()
print(response)
else:
print(response)
except requests.ConnectionError as e:
print('Error', e.args)

def analysis(dump):
data={}
data['deviceName']=dump.split()[3]
data['processName']=re.match('^[a-zA-Z\.]*',dump.split()[4]).group()
data['processId'] = dump.split('[')[1].split(']')[0]
data['description']=' '.join(dump.split()[5:-1])
hour=re.match('^([0-9]{2})',dump.split()[2]).group()
if int(hour)<9:
data['timeWindow']='0'+hour[1]+'00-0'+str(int(hour)+1)+'00'
elif int(hour)==9:
data['timeWindow'] ='0'+hour + '00-' + str(int(hour) + 1) + '00'
else:
data['timeWindow'] = hour + '00-' + str(int(hour) + 1) + '00'
return data

if __name__=='__main__':
logpath='.\interview_data_set'
url='https://foo.com/bar'
main(logpath,url)
Loading