Logging Data

We use ping to estimate network congestion in our aging LAN and log it periodically to a wiki page.

See Air Temperature which serves as a model.

We begin by writing a page in the form that we want.

We show that D3 Line can render this form.

Then we write the scripts that collect data and write it on this page. For convenience, we'll fork this page to our laptop and develop code there. We'll use git to record our work and deploy it back to this server.

We begin with functions to fetch and store pages given a path into the flat-file store. We'll convert from and to JSON as we read and write.

fetchPage = (path, done) -> fs.readFile path, 'utf8', (err, data) -> done JSON.parse data storePage = (path, page, done) -> data = JSON.stringify page, null, ' ' fs.writeFile path, data, 'utf8', done

We'll find the the data (type chart) and update it in place.

findChart = (page) -> for item in page.story return item if item.type == 'chart' updateChart = (slug, data) -> path = "../pages/#{slug}" fetchPage path, (page) -> chart = findChart page sample = [new Date().getTime(), data] chart.data.shift() chart.data.push sample storePage path, page

We'll record data that we get from ping. We have to find the numbers we want in ping's text output.

Finally we will write a small main program that uses these functions to measure response times to our router and log them on this page.

findStats = (text) -> regex = /([0-9.]+)\/([0-9.]+)\/([0-9.]+)/ for line in text.split /\n/ if n = line.match regex return min:+n[1], avg:+n[2], max:+n[3] pingSummary = (addr, done) -> cmd = "/bin/ping -q -c 60 #{addr}" child.exec cmd, (error, stdout, stderr) -> console.log stderr if stderr done findStats stdout

pingSummary '10.0.3.1', (summary) -> updateChart 'logging-data', summary.avg

We run this script from cron starting a new copyevery minute. Since it takes about a minute to run it means we're pinging pretty much continuously.