From 2ec4ac33b27e751b6dfad3260bf9118b98edd290 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 3 Apr 2020 21:49:33 +0200 Subject: [PATCH] scraping the xerox printers --- Gemfile | 5 ++ Gemfile.lock | 44 +++++++++++++ default.nix | 27 ++++++++ gemset.nix | 169 ++++++++++++++++++++++++++++++++++++++++++++++++ xerox/scrape.rb | 65 +++++++++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 default.nix create mode 100644 gemset.nix create mode 100644 xerox/scrape.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a458566 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem "mechanize", "~> 2.7" +gem "open4" +gem "influxdb", "~> 0.8" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..7fb470a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,44 @@ +GEM + remote: https://rubygems.org/ + specs: + connection_pool (2.2.2) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) + http-cookie (1.0.3) + domain_name (~> 0.5) + influxdb (0.8.0) + mechanize (2.7.6) + domain_name (~> 0.5, >= 0.5.1) + http-cookie (~> 1.0) + mime-types (>= 1.17.2) + net-http-digest_auth (~> 1.1, >= 1.1.1) + net-http-persistent (>= 2.5.2) + nokogiri (~> 1.6) + ntlm-http (~> 0.1, >= 0.1.1) + webrobots (>= 0.0.9, < 0.2) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2019.1009) + mini_portile2 (2.4.0) + net-http-digest_auth (1.4.1) + net-http-persistent (3.1.0) + connection_pool (~> 2.2) + nokogiri (1.10.9) + mini_portile2 (~> 2.4.0) + ntlm-http (0.1.1) + open4 (1.3.4) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.7) + webrobots (0.1.2) + +PLATFORMS + ruby + +DEPENDENCIES + influxdb (~> 0.8) + mechanize (~> 2.7) + open4 + +BUNDLED WITH + 1.17.3 diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..21cb278 --- /dev/null +++ b/default.nix @@ -0,0 +1,27 @@ +{ pkgs ? import {} }: + +with pkgs; + +let + gems = bundlerEnv rec { + name = "scrapers"; + + inherit ruby; + # expects Gemfile, Gemfile.lock and gemset.nix in the same directory + # > nix-shell -p bundler --run 'bundle lock --update' + # > nix-shell -p bundix --run bundix + gemdir = ./.; + }; + wrapScript = name: script: + stdenv.mkDerivation { + inherit name script; + buildInputs = [ gems.wrappedRuby ]; + + buildCommand = '' + install -D -m755 $script $out/bin/${name} + patchShebangs $out/bin/${name} + ''; + }; +in { + xerox = wrapScript "xerox" ./xerox/scrape.rb; +} diff --git a/gemset.nix b/gemset.nix new file mode 100644 index 0000000..2d7e1af --- /dev/null +++ b/gemset.nix @@ -0,0 +1,169 @@ +{ + connection_pool = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0lflx29mlznf1hn0nihkgllzbj8xp5qasn8j7h838465pi399k68"; + type = "gem"; + }; + version = "2.2.2"; + }; + domain_name = { + dependencies = ["unf"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0lcqjsmixjp52bnlgzh4lg9ppsk52x9hpwdjd53k8jnbah2602h0"; + type = "gem"; + }; + version = "0.5.20190701"; + }; + http-cookie = { + dependencies = ["domain_name"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "004cgs4xg5n6byjs7qld0xhsjq3n6ydfh897myr2mibvh6fjc49g"; + type = "gem"; + }; + version = "1.0.3"; + }; + influxdb = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "11q5pys13cp9rxhg1gihblqi597b6nfsfz3ghdij0zdf713pwwc3"; + type = "gem"; + }; + version = "0.8.0"; + }; + mechanize = { + dependencies = ["domain_name" "http-cookie" "mime-types" "net-http-digest_auth" "net-http-persistent" "nokogiri" "ntlm-http" "webrobots"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00834zv6s68g3x00s4kcgdry51s88qj0kjygnwhpg8k67kdihcrx"; + type = "gem"; + }; + version = "2.7.6"; + }; + mime-types = { + dependencies = ["mime-types-data"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1zj12l9qk62anvk9bjvandpa6vy4xslil15wl6wlivyf51z773vh"; + type = "gem"; + }; + version = "3.3.1"; + }; + mime-types-data = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "18x61fc36951vw7f74gq8cyybdpxvyg5d0azvqhrs82ddw3v16xh"; + type = "gem"; + }; + version = "3.2019.1009"; + }; + mini_portile2 = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "15zplpfw3knqifj9bpf604rb3wc1vhq6363pd6lvhayng8wql5vy"; + type = "gem"; + }; + version = "2.4.0"; + }; + net-http-digest_auth = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1nq859b0gh2vjhvl1qh1zrk09pc7p54r9i6nnn6sb06iv07db2jb"; + type = "gem"; + }; + version = "1.4.1"; + }; + net-http-persistent = { + dependencies = ["connection_pool"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1bink2g0a94zh9kmb96rrh48rkbkdpimljy2370z5liixvy3gdpa"; + type = "gem"; + }; + version = "3.1.0"; + }; + nokogiri = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "12j76d0bp608932xkzmfi638c7aqah57l437q8494znzbj610qnm"; + type = "gem"; + }; + version = "1.10.9"; + }; + ntlm-http = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0yx01ffrw87wya1syivqzf8hz02axk7jdpw6aw221xwvib767d36"; + type = "gem"; + }; + version = "0.1.1"; + }; + open4 = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1cgls3f9dlrpil846q0w7h66vsc33jqn84nql4gcqkk221rh7px1"; + type = "gem"; + }; + version = "1.3.4"; + }; + unf = { + dependencies = ["unf_ext"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0bh2cf73i2ffh4fcpdn9ir4mhq8zi50ik0zqa1braahzadx536a9"; + type = "gem"; + }; + version = "0.1.4"; + }; + unf_ext = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0wc47r23h063l8ysws8sy24gzh74mks81cak3lkzlrw4qkqb3sg4"; + type = "gem"; + }; + version = "0.0.7.7"; + }; + webrobots = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "19ndcbba8s8m62hhxxfwn83nax34rg2k5x066awa23wknhnamg7b"; + type = "gem"; + }; + version = "0.1.2"; + }; +} \ No newline at end of file diff --git a/xerox/scrape.rb b/xerox/scrape.rb new file mode 100644 index 0000000..fae29b3 --- /dev/null +++ b/xerox/scrape.rb @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby + +require 'open-uri' +require 'influxdb' + +HOST = ARGV[0] +USER = ARGV[1] +PASSWORD = ARGV[2] + +def scrape_paper_supply + results = {} + + open("http://#{HOST}/sttray.htm", :http_basic_authentication => [USER, PASSWORD]) + .read() + .scan(/\['Tray (.+?)',\d+,(\d+),/) do |m| + + results[m[0]] = m[1].to_i + end + + results +end + +def scrape_consumables + results = {} + + doc = open("http://#{HOST}/stsply.htm", :http_basic_authentication => [USER, PASSWORD]) + .read() + .force_encoding("ISO-8859-1") + .encode("utf-8", replace: nil) + + doc.scan(/info.concat\(\[\[(.+?)\);/) do |m| + m[0].scan(/\['(.+?)',(\d+),(\d+)\]/) do |m| + results[m[0]] = { + :status => m[1].to_i, + :life => m[2].to_i, + } + end + end + + results +end + +paper_supply = scrape_paper_supply +consumables = scrape_consumables + +db = InfluxDB::Client.new(url: "http://grafana.hq.c3d2.de:8086/drucker") +db.write_points [ { + series: "paper_supply", + tags: { host: HOST }, + values: paper_supply, +}, { + series: "consumables_status", + tags: { host: HOST }, + values: consumables.inject({}) do |status, consumable| + status[consumable[0]] = consumable[1][:status] + status + end, +}, { + series: "consumables_life", + tags: { host: HOST }, + values: consumables.inject({}) do |life, consumable| + life[consumable[0]] = consumable[1][:life] + life + end, +} ], 'm'