전체 글
-
[ Python ] Prometheus metric 값 가져오기Python/Python Programming 2023. 5. 24. 13:56
Python을이용하여 promQL 쿼리하여 값 가져오기 Sample Metric : jvm_memory_used_bytes import requests from datetime import datetime instance = '127.0.0.1:8080' job = 'spring' query = 'sum(jvm_memory_used_bytes{area="heap", instance="%s", job="%s"})' % (instance,job) def prometheus_data(instance,jop,query): response = requests.get('http://127.0.0.1:9090/api/v1/query', params={'query': query}) response = response...
-
[ Python ] Linux 파일 (스토리지) 연도별 개수와 총 용량 구하기Python/Python Programming 2023. 5. 24. 00:22
from os import walk from os.path import getsize from os.path import getmtime from datetime import datetime from os import chdir chdir(path) resultFile = 'result.csv' for (path, dir, files) in walk('.'): for filename in files: ext = filename.split('.')[-1] size = getsize(path + '\\' + filename) mtime = str(datetime.fromtimestamp(getmtime(path + '\\' + filename)))[:19] lst = '#'.join([ path, filen..
-
[ Python ] pandas plot 을 이용한 다양한 graph 그리기Python/Python For Analytics 2023. 5. 24. 00:01
Pandas의 plot 을 이용하여 그래프 그리기 import numpy as np import pandas as pd df = pd.DataFrame(np.random.rand(20,6), columns=['a','b','c','d','e','f']) line graph df.plot.line(figsize = (15,5)) bar graph df.plot.bar(figsize = (15,5), grid=True) area graph df.plot.area(figsize = (15,5), xticks = (1,5,10,15,20), yticks = (1,2,3,4,5)) area graph ( Time index ) import numpy as np import pandas as pd from date..
-
[ Python ] socket 모듈을 이용한 Port open / close checkPython/Python Programming 2023. 5. 23. 17:49
import socket from datetime import datetime import time checkTime = str(datetime.today())[:19] ipadd = '127.0.0.1' # IP Address, string port = 80 # Port Number, interger sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) res = sock.connect_ex((ipadd, port)) text = str(port) + ' : ' + checkTime print('Open ' + text if res == 0 else 'Close ' + text) -----------------------..
-
[ AWS ] Python boto3를 이용한 S3에 저장된 CSV 파일 읽기 / 쓰기AWS Infra 2023. 5. 23. 14:56
CSV파일 S3에 다이렉트 저장하기 Sample : Pandas DataFrame ( 24 X 5 ) from io import StringIO import boto3 s3 = boto3.client('s3') bucket = 'string' # Put Bucket Name csv_buffer = StringIO() df.to_csv(csv_buffer) s3.put_object(Bucket=bucket, Body=csv_buffer.getvalue(), Key='df.csv') S3에 저장되어 있는 CSV파일 다이렉트 읽기 import boto3 import pandas as pd import io s3 = boto3.client('s3') bucket = 'string' obj = s3.get_obj..
-
[ AWS ] Python boto3를 이용한 EC2 Instance 정보 구하기AWS Infra 2023. 5. 22. 14:10
Python Boto3를 이용하여 EC2의 정보를 구해보자. ISMS 등 자산관리 및 EC2의 기본적인 모니터링을 할 때도 유용하게 사용할 수 있다. import boto3 ec2 = boto3.client('ec2') def ec2_info_func(): ec2_info = {} response = ec2.describe_instances() for reservation in response["Reservations"]: for i in reservation["Instances"]: for j in i['Tags']: # tagName이 없으면 'None' tagName = 'None' if j['Key'] == 'Name': tagName = j['Value'] ec2_info[i["InstanceI..
-
[ Python ] pandas DataFrame을 HTML 형식으로 export 하기. (모니터링 활용)Python/Python Programming 2023. 5. 17. 18:22
pandas의 to_html 메소드를 이용하여 DataFrame 결과를 HTML 형식으로 output kor, math, eng 3개 컬럼의 기본 DataFrame 만들기 kor = [99, 53, 56, 56, 81, 90, 67, 68, 83, 55] math = [91, 77, 59, 70, 100, 67, 89, 55, 93, 99] eng = [96, 92, 92, 65, 51, 92, 55, 60, 54, 51] import pandas as pd df = pd.DataFrame(zip(kor,math,eng), columns=['kor','math','eng']) df html = df.to_html() print(html) kor math eng 0 99 91 96 1 53 77 92 2 ..
-
[ AWS ] Python boto3를 이용하여 WAF IPset IP 수정하기AWS Infra 2023. 5. 16. 17:28
Python Boto3를 이용한 WAF IPset에 IP Address 추가 * 하나의 IP를 추가/삭제는 않되기 때문에, 기존 IP 리스트를 가져와서 수정하는 것 import boto3 client = boto3.client('wafv2') ipsetName = 'string' ipsetId = 'string' ipAddress = 'x.x.x./32' def wafv2_update_ip_set(n,i,a): # ip가 포함되어 있는지 get_ip_set으로 확인 response = client.get_ip_set( Name=n, Scope='REGIONAL', Id=i) if a in response['IPSet']['Addresses']: return 'exist ipaddress' else: # ..