Skip to content

Commit 0173cf3

Browse files
committed
wip
1 parent 7c3311d commit 0173cf3

1 file changed

Lines changed: 134 additions & 133 deletions

File tree

  • .github/actions/log_to_splunk

.github/actions/log_to_splunk/main.py

Lines changed: 134 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -8,139 +8,140 @@
88
from datetime import datetime
99

1010
def main():
11-
12-
GITHUB_REF=os.environ["GITHUB_REF"]
13-
GITHUB_REPOSITORY=os.environ["GITHUB_REPOSITORY"]
14-
GITHUB_RUN_ID=os.environ["GITHUB_RUN_ID"]
15-
GITHUB_API_URL=os.environ["GITHUB_API_URL"]
16-
GITHUB_WORKFLOWID=os.environ["INPUT_WORKFLOWID"]
17-
GITHUB_TOKEN = os.environ.get("INPUT_GITHUB-TOKEN")
18-
19-
SPLUNK_HEC_URL=os.environ["INPUT_SPLUNK-URL"]+"services/collector/event"
20-
SPLUNK_HEC_TOKEN=os.environ["INPUT_HEC-TOKEN"]
21-
SPLUNK_SOURCE=os.environ["INPUT_SOURCE"]
22-
SPLUNK_SOURCETYPE=os.environ["INPUT_SOURCETYPE"]
23-
24-
batch = count = 0
25-
eventBatch = ""
26-
headers = {"Authorization": "Splunk "+SPLUNK_HEC_TOKEN}
27-
host=os.uname()[1]
28-
29-
summary_url = "{url}/repos/{repo}/actions/runs/{run_id}".format(url=GITHUB_API_URL,repo=GITHUB_REPOSITORY,run_id=GITHUB_WORKFLOWID)
30-
31-
try:
32-
x = requests.get(summary_url, stream=True, auth=('token',GITHUB_TOKEN))
33-
x.raise_for_status()
34-
except requests.exceptions.HTTPError as errh:
35-
output = "GITHUB API Http Error:" + str(errh)
36-
print(f"Error: {output}")
37-
print(f"::set-output name=result::{output}")
38-
return x.status_code
39-
except requests.exceptions.ConnectionError as errc:
40-
output = "GITHUB API Error Connecting:" + str(errc)
41-
print(f"Error: {output}")
42-
print(f"::set-output name=result::{output}")
43-
return x.status_code
44-
except requests.exceptions.Timeout as errt:
45-
output = "Timeout Error:" + str(errt)
46-
print(f"Error: {output}")
47-
print(f"::set-output name=result::{output}")
48-
return x.status_code
49-
except requests.exceptions.RequestException as err:
50-
output = "GITHUB API Non catched error conecting:" + str(err)
51-
print(f"Error: {output}")
52-
print(f"::set-output name=result::{output}")
53-
return x.status_code
54-
except Exception as e:
55-
print("Internal error", e)
56-
return x.status_code
57-
58-
summary = x.json()
59-
60-
summary.pop('repository')
61-
62-
summary["repository"]=summary["head_repository"]["name"]
63-
summary["repository_full"]=summary["head_repository"]["full_name"]
64-
65-
summary.pop('head_repository')
66-
67-
utc_time = datetime.strptime(summary["updated_at"], "%Y-%m-%dT%H:%M:%SZ")
68-
epoch_time = (utc_time - datetime(1970, 1, 1)).total_seconds()
69-
70-
event={'event':json.dumps(summary),'sourcetype':SPLUNK_SOURCETYPE,'source':'workflow_summary','host':host,'time':epoch_time}
71-
event=json.dumps(event)
72-
73-
x=requests.post(SPLUNK_HEC_URL, data=event, headers=headers)
74-
75-
76-
url = "{url}/repos/{repo}/actions/runs/{run_id}/logs".format(url=GITHUB_API_URL,repo=GITHUB_REPOSITORY,run_id=GITHUB_WORKFLOWID)
77-
print(url)
78-
79-
try:
80-
x = requests.get(url, stream=True, auth=('token',GITHUB_TOKEN))
81-
82-
except requests.exceptions.HTTPError as errh:
83-
output = "GITHUB API Http Error:" + str(errh)
84-
print(f"Error: {output}")
85-
print(f"::set-output name=result::{output}")
86-
return
87-
except requests.exceptions.ConnectionError as errc:
88-
output = "GITHUB API Error Connecting:" + str(errc)
89-
print(f"Error: {output}")
90-
print(f"::set-output name=result::{output}")
91-
return
92-
except requests.exceptions.Timeout as errt:
93-
output = "Timeout Error:" + str(errt)
94-
print(f"Error: {output}")
95-
print(f"::set-output name=result::{output}")
96-
return
97-
except requests.exceptions.RequestException as err:
98-
output = "GITHUB API Non catched error conecting:" + str(err)
99-
print(f"Error: {output}")
100-
print(f"::set-output name=result::{output}")
101-
return
102-
103-
z = zipfile.ZipFile(io.BytesIO(x.content))
104-
z.extractall('/app')
105-
106-
timestamp = batch = count = 0
107-
108-
for name in glob.glob('/app/*.txt'):
109-
logfile = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), name.replace('./','')),'r')
110-
Lines = logfile.readlines()
111-
for line in Lines:
112-
113-
if line:
114-
count+=1
115-
if timestamp:
116-
t2=timestamp
117-
timestamp = re.search("\d{4}-\d{2}-\d{2}T\d+:\d+:\d+.\d+Z",line.strip())
118-
119-
if timestamp:
120-
timestamp = re.sub("\dZ","",timestamp.group())
121-
timestamp = datetime.strptime(timestamp,"%Y-%m-%dT%H:%M:%S.%f")
122-
timestamp = (timestamp - datetime(1970,1,1)).total_seconds()
123-
else:
124-
timestamp=t2
125-
126-
x = re.sub("\d{4}-\d{2}-\d{2}T\d+:\d+:\d+.\d+Z","",line.strip())
127-
x=x.strip()
128-
job_name=re.search("\/\d+\_(?P<job>.*)\.txt",name)
129-
job_name=job_name.group('job')
130-
fields = {'lineNumber':count,'workflowID':GITHUB_WORKFLOWID,'job':job_name}
131-
if x:
132-
batch+=1
133-
event={'event':x,'sourcetype':SPLUNK_SOURCETYPE,'source':SPLUNK_SOURCE,'host':host,'time':timestamp,'fields':fields}
134-
eventBatch=eventBatch+json.dumps(event)
135-
else:
136-
print("skipped line "+str(count))
137-
138-
if batch>=1000:
139-
batch=0
140-
x=requests.post(SPLUNK_HEC_URL, data=eventBatch, headers=headers)
141-
eventBatch=""
142-
143-
x=requests.post(SPLUNK_HEC_URL, data=eventBatch, headers=headers)
11+
print("######################")
12+
13+
# GITHUB_REF=os.environ["GITHUB_REF"]
14+
# GITHUB_REPOSITORY=os.environ["GITHUB_REPOSITORY"]
15+
# GITHUB_RUN_ID=os.environ["GITHUB_RUN_ID"]
16+
# GITHUB_API_URL=os.environ["GITHUB_API_URL"]
17+
# GITHUB_WORKFLOWID=os.environ["INPUT_WORKFLOWID"]
18+
# GITHUB_TOKEN = os.environ.get("INPUT_GITHUB-TOKEN")
19+
20+
# SPLUNK_HEC_URL=os.environ["INPUT_SPLUNK-URL"]+"services/collector/event"
21+
# SPLUNK_HEC_TOKEN=os.environ["INPUT_HEC-TOKEN"]
22+
# SPLUNK_SOURCE=os.environ["INPUT_SOURCE"]
23+
# SPLUNK_SOURCETYPE=os.environ["INPUT_SOURCETYPE"]
24+
25+
# batch = count = 0
26+
# eventBatch = ""
27+
# headers = {"Authorization": "Splunk "+SPLUNK_HEC_TOKEN}
28+
# host=os.uname()[1]
29+
30+
# summary_url = "{url}/repos/{repo}/actions/runs/{run_id}".format(url=GITHUB_API_URL,repo=GITHUB_REPOSITORY,run_id=GITHUB_WORKFLOWID)
31+
32+
# try:
33+
# x = requests.get(summary_url, stream=True, auth=('token',GITHUB_TOKEN))
34+
# x.raise_for_status()
35+
# except requests.exceptions.HTTPError as errh:
36+
# output = "GITHUB API Http Error:" + str(errh)
37+
# print(f"Error: {output}")
38+
# print(f"::set-output name=result::{output}")
39+
# return x.status_code
40+
# except requests.exceptions.ConnectionError as errc:
41+
# output = "GITHUB API Error Connecting:" + str(errc)
42+
# print(f"Error: {output}")
43+
# print(f"::set-output name=result::{output}")
44+
# return x.status_code
45+
# except requests.exceptions.Timeout as errt:
46+
# output = "Timeout Error:" + str(errt)
47+
# print(f"Error: {output}")
48+
# print(f"::set-output name=result::{output}")
49+
# return x.status_code
50+
# except requests.exceptions.RequestException as err:
51+
# output = "GITHUB API Non catched error conecting:" + str(err)
52+
# print(f"Error: {output}")
53+
# print(f"::set-output name=result::{output}")
54+
# return x.status_code
55+
# except Exception as e:
56+
# print("Internal error", e)
57+
# return x.status_code
58+
59+
# summary = x.json()
60+
61+
# summary.pop('repository')
62+
63+
# summary["repository"]=summary["head_repository"]["name"]
64+
# summary["repository_full"]=summary["head_repository"]["full_name"]
65+
66+
# summary.pop('head_repository')
67+
68+
# utc_time = datetime.strptime(summary["updated_at"], "%Y-%m-%dT%H:%M:%SZ")
69+
# epoch_time = (utc_time - datetime(1970, 1, 1)).total_seconds()
70+
71+
# event={'event':json.dumps(summary),'sourcetype':SPLUNK_SOURCETYPE,'source':'workflow_summary','host':host,'time':epoch_time}
72+
# event=json.dumps(event)
73+
74+
# x=requests.post(SPLUNK_HEC_URL, data=event, headers=headers)
75+
76+
77+
# url = "{url}/repos/{repo}/actions/runs/{run_id}/logs".format(url=GITHUB_API_URL,repo=GITHUB_REPOSITORY,run_id=GITHUB_WORKFLOWID)
78+
# print(url)
79+
80+
# try:
81+
# x = requests.get(url, stream=True, auth=('token',GITHUB_TOKEN))
82+
83+
# except requests.exceptions.HTTPError as errh:
84+
# output = "GITHUB API Http Error:" + str(errh)
85+
# print(f"Error: {output}")
86+
# print(f"::set-output name=result::{output}")
87+
# return
88+
# except requests.exceptions.ConnectionError as errc:
89+
# output = "GITHUB API Error Connecting:" + str(errc)
90+
# print(f"Error: {output}")
91+
# print(f"::set-output name=result::{output}")
92+
# return
93+
# except requests.exceptions.Timeout as errt:
94+
# output = "Timeout Error:" + str(errt)
95+
# print(f"Error: {output}")
96+
# print(f"::set-output name=result::{output}")
97+
# return
98+
# except requests.exceptions.RequestException as err:
99+
# output = "GITHUB API Non catched error conecting:" + str(err)
100+
# print(f"Error: {output}")
101+
# print(f"::set-output name=result::{output}")
102+
# return
103+
104+
# z = zipfile.ZipFile(io.BytesIO(x.content))
105+
# z.extractall('/app')
106+
107+
# timestamp = batch = count = 0
108+
109+
# for name in glob.glob('/app/*.txt'):
110+
# logfile = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), name.replace('./','')),'r')
111+
# Lines = logfile.readlines()
112+
# for line in Lines:
113+
114+
# if line:
115+
# count+=1
116+
# if timestamp:
117+
# t2=timestamp
118+
# timestamp = re.search("\d{4}-\d{2}-\d{2}T\d+:\d+:\d+.\d+Z",line.strip())
119+
120+
# if timestamp:
121+
# timestamp = re.sub("\dZ","",timestamp.group())
122+
# timestamp = datetime.strptime(timestamp,"%Y-%m-%dT%H:%M:%S.%f")
123+
# timestamp = (timestamp - datetime(1970,1,1)).total_seconds()
124+
# else:
125+
# timestamp=t2
126+
127+
# x = re.sub("\d{4}-\d{2}-\d{2}T\d+:\d+:\d+.\d+Z","",line.strip())
128+
# x=x.strip()
129+
# job_name=re.search("\/\d+\_(?P<job>.*)\.txt",name)
130+
# job_name=job_name.group('job')
131+
# fields = {'lineNumber':count,'workflowID':GITHUB_WORKFLOWID,'job':job_name}
132+
# if x:
133+
# batch+=1
134+
# event={'event':x,'sourcetype':SPLUNK_SOURCETYPE,'source':SPLUNK_SOURCE,'host':host,'time':timestamp,'fields':fields}
135+
# eventBatch=eventBatch+json.dumps(event)
136+
# else:
137+
# print("skipped line "+str(count))
138+
139+
# if batch>=1000:
140+
# batch=0
141+
# x=requests.post(SPLUNK_HEC_URL, data=eventBatch, headers=headers)
142+
# eventBatch=""
143+
144+
# x=requests.post(SPLUNK_HEC_URL, data=eventBatch, headers=headers)
144145

145146
if __name__ == '__main__':
146147
main()

0 commit comments

Comments
 (0)