Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active April 1, 2022 23:10
Show Gist options
  • Save ericboehs/c827ca408b7a0dd55dcb33ba3b5b3ca8 to your computer and use it in GitHub Desktop.
Save ericboehs/c827ca408b7a0dd55dcb33ba3b5b3ca8 to your computer and use it in GitHub Desktop.
Raspberry Pi Locator
#! /usr/bin/env ruby
require 'rss'
require 'open-uri'
SLEEP = 30
url = 'https://rpilocator.com/feed.rss'
db = nil
# Print cpu/memory usage in bytes when receiving SIGINFO
trap 'INFO' do
current_time = Time.now.strftime '%Y-%m-%dT%H:%M:%S'
cpu, mem = `ps -o %cpu,rss #{Process.pid} | tail -1 | awk '{print $1,$2}'`.chomp.split
puts "#{current_time} %CPU: #{cpu}; MEM_RSS:#{mem}"
end
loop.with_index do |_, i|
begin
URI.open(url) do |rss|
feed = RSS::Parser.parse rss
db_items = db&.items&.map { |item| { title: item.title, pubDate: item.pubDate } } || []
feed_items = feed.items.map { |item| { title: item.title, pubDate: item.pubDate } }
new_items = feed_items - db_items
new_titles = new_items&.map { |item| item[:title] }
skip_notification = true unless db
db = feed
if new_titles.any? { |title| title.include?('Stock Alert (US)') }
next if skip_notification
message = new_items.map do |item|
"<https://rpilocator.com/?country=US|#{item[:title]}> - #{item[:pubDate]}"
end.join("\n\n")
`slack-noti "#{message}"`
end
end
rescue StandardError
end
# Print memory usage every 15 minutes (using SIGINFO)
fifteen_minutes_elapsed = i % (15 * 60 / SLEEP) == 0
Process.kill 'INFO', Process.pid if fifteen_minutes_elapsed
sleep SLEEP
end
@ericboehs
Copy link
Author

ericboehs commented Mar 24, 2022

Install and run:

curl -L https://gist.github.com/ericboehs/c827ca408b7a0dd55dcb33ba3b5b3ca8/raw/rpi > rpi
chmod +x rpi
rpi; slack-noti 'RPi Exited. You should restart it.'

Recommended: slack-noti.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment