Last active
January 28, 2025 03:57
-
-
Save keisukefukuda/31271d58f83acd49b1ff8673badf9161 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
api_token = "3194646246-8381798388480-knIxqKpkvCszsCN771p....." | |
channel_id = "N73D8.." | |
import http.client | |
import json | |
import getpass | |
import subprocess | |
import time | |
def post_message_to_slack(api_token, channel_id, message): | |
# Slack APIのエンドポイント | |
host = "slack.com" | |
path = "/api/chat.postMessage" | |
# HTTPヘッダー | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {api_token}" | |
} | |
# リクエストボディ | |
body = { | |
"channel": channel_id, | |
"text": message | |
} | |
# JSON形式のボディをエンコード | |
json_body = json.dumps(body) | |
# HTTPS接続の作成 | |
connection = http.client.HTTPSConnection(host) | |
try: | |
# POSTリクエストを送信 | |
connection.request("POST", path, body=json_body, headers=headers) | |
# レスポンスの取得 | |
response = connection.getresponse() | |
response_data = response.read().decode("utf-8") | |
# レスポンスを解析して結果を表示 | |
result = json.loads(response_data) | |
if result.get("ok"): | |
print("Message posted successfully!") | |
else: | |
print("Failed to post message:", result.get("error")) | |
except Exception as e: | |
print("An error occurred:", e) | |
finally: | |
# 接続を閉じる | |
connection.close() | |
N = 5 | |
token_last_chars= getpass.getpass(prompt=f"Token last {N} chars: ") | |
assert len(token_last_chars) == N | |
api_token = "xoxb-" + api_token[:-N] + token_last_chars | |
N = 2 | |
channel_last_chars = getpass.getpass(prompt=f"Channel last {N} chars: ") | |
assert len(channel_last_chars) == N | |
channel_id = "CC" + channel_id[:-N] + channel_last_chars | |
# intervalを2倍しながら、最長1時間ごとにPostする | |
interval = 10 # sec | |
while True: | |
message = "" | |
# メッセージを構築 | |
try: | |
message += subprocess.check_output(['bash', '-c', 'df -h | grep Elem'], encoding="utf-8") | |
except Exception as e: | |
message += f"Failed to get disk usage info: {e}" | |
for log_file in ["pfn-copy-progress.log", "pfn-copy-error.log"]: | |
message += "\n\n----\n\n" | |
try: | |
message += f"Last 10 lines of /home/bee/{log_file}:\n" | |
message += "\n".join(open(f"/home/bee/{log_file}", 'r', encoding="utf-8").readlines()[-10:]) | |
except Exception as e: | |
message += f"Failed to read {log_file}: {e}" | |
# find でファイルの個数を数える | |
message += "\n\n---- ファイル個数\n\n" | |
day_dirs = subprocess.check_output(['find', '/run/media/bee/Elements/bosai-spk/2024/', '-maxdepth', '2', '-mindepth', '2', '-type', 'd'], encoding="utf-8").split() | |
for d in day_dirs: | |
count = int(subprocess.check_output(['bash', '-c', f"find {d} -type f -name '*.spk.gz' | wc -l"], encoding="utf-8")) | |
message += f"{d}: {count}\n" | |
print(message) | |
break | |
post_message_to_slack(api_token, channel_id, message) | |
time.sleep(interval) | |
interval = min(interval * 2, 3600) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment