Skip to content

Instantly share code, notes, and snippets.

@mlapida
Created January 10, 2023 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlapida/883f32dcc6e21c24e3a1625f1b71f034 to your computer and use it in GitHub Desktop.
Save mlapida/883f32dcc6e21c24e3a1625f1b71f034 to your computer and use it in GitHub Desktop.
A small python script that will publish some basic local stats to Mastodon (or GoToSocial). Can be put on a cron to do so regularly.
import requests
import psutil
import time
# A small function to calculate updtime.
def seconds_elapsed():
return time.time() - psutil.boot_time()
requests.post(
# It's running on the server, so it can use the local endpoint. This is for GoToSocial. You can use the public endpoint as well.
url="http://localhost:8080/api/v1/statuses",
headers={
# For GoToSocial, you'll need to find a way to pull a token, such as https://takahashim.github.io/mastodon-access-token/
"Authorization": "Bearer <my access token>"
},
json={
# Send the CPU load, the memory %, the load averages, and the uptime. Format it in Markdown so it renders the line breaks properly across clients.
"status": "Healthcheck! \nCPU: " + str(psutil.cpu_percent(4)) + "% \nMemory: " +
str(psutil.virtual_memory()[2]) + "% \nLoad: " + str(psutil.getloadavg()) + "\nUptime: " +
str(round((seconds_elapsed()/60)/60)) + " hours", "visibility": "public", "format": "markdown"},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment