Last active
May 28, 2020 18:23
-
-
Save sulami/606fa4dcbfa1a21513cd45a03cba559c to your computer and use it in GitHub Desktop.
Check the Internet Connection
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
#!/usr/bin/env racket | |
#lang racket | |
(require racket/date) | |
(require net/http-client) | |
(define (print-status now online [reason #f]) | |
(if reason | |
(printf "date=~a online=~s reason=~s\n" | |
now | |
online | |
reason) | |
(printf "date=~a online=~s\n" | |
now | |
online))) | |
(define (check-internet) | |
(define now-seconds (current-seconds)) | |
(define this-thread (current-thread)) | |
(define status | |
(begin | |
(sync/timeout/enable-break | |
5 | |
(thread | |
(lambda () | |
(with-handlers ([exn:fail? | |
(lambda (e) | |
(thread-send this-thread | |
'#:connection-fail))]) | |
(define conn (http-conn-open "google.com" | |
#:ssl? #t | |
#:port 443)) | |
(http-conn-send! conn "/") | |
(define-values (http-status headers port) | |
(http-conn-recv! conn | |
#:close? #t)) | |
(thread-send this-thread http-status))))) | |
(let ((result (or (thread-try-receive) | |
'#:timeout))) | |
(cond | |
((keyword? result) | |
result) | |
((not (equal? #"HTTP/1.1 301 Moved Permanently" result)) | |
'#:wrong-status) | |
(else | |
#t))))) | |
(define now (date->string (seconds->date now-seconds) #t)) | |
(if (equal? #t status) | |
(print-status now #t) | |
(print-status now #f status)) | |
(with-output-to-file "/Users/sulami/internet" | |
(lambda () | |
(if (equal? #t status) | |
(print-status now #t) | |
(print-status now #f status))) | |
#:exists 'append) | |
(sleep 60) | |
(check-internet)) | |
(date-display-format 'iso-8601) | |
(check-internet) |
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
#!/bin/sh | |
#|-*- mode:lisp -*-|# | |
#| | |
exec ros -Q -- $0 "$@" | |
|# | |
(progn ;;init forms | |
(ros:ensure-asdf) | |
#+quicklisp(ql:quickload '(dexador trivial-timeout) :silent t) | |
) | |
(defpackage :ros.script.internetcheck.3799674981 | |
(:use :cl)) | |
(in-package :ros.script.internetcheck.3799674981) | |
(defconstant PING-HOST "https://google.com") | |
(defun online-status () | |
(handler-case | |
(trivial-timeout:with-timeout (5) | |
(dex:get PING-HOST) | |
:ok) | |
(dex:http-request-failed (e) | |
(print e) | |
(terpri) | |
:request-failed) | |
(trivial-timeout:timeout-error (e) | |
(print e) | |
(terpri) | |
:timeout) | |
(error (e) | |
(print e) | |
(terpri) | |
:error))) | |
(defun format-log-line (time status) | |
(if (equal :ok status) | |
(format nil "date=~a online=true~%" | |
time) | |
(format nil "date=~a online=false reason=~a~%" | |
time | |
status))) | |
(defun write-log-entry (entry) | |
(with-open-file (stream "~/internet2" | |
:direction :output | |
:if-exists :append) | |
(format stream entry))) | |
(defun now-timestamp () | |
(local-time:format-timestring nil (local-time:now))) | |
(defun check-internet () | |
(let ((log-entry (format-log-line | |
(now-timestamp) | |
(online-status)))) | |
(format t log-entry) | |
(write-log-entry log-entry))) | |
(defun main (&rest argv) | |
(declare (ignorable argv)) | |
(handler-case | |
(loop do (check-internet) | |
(sleep 5)) | |
(sb-sys:interactive-interrupt () nil))) | |
;;; vim: set ft=lisp lisp: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment