Browse Source

Initial commit

Rune 1 year ago
commit
0daa506a3e
7 changed files with 257 additions and 0 deletions
  1. 58
    0
      .gitignore
  2. 0
    0
      .npmignore
  3. 21
    0
      LICENSE
  4. 71
    0
      MMM-ping.js
  5. 50
    0
      README.md
  6. 25
    0
      node_helper.js
  7. 32
    0
      package.json

+ 58
- 0
.gitignore View File

@@ -0,0 +1,58 @@
1
+# Logs
2
+logs
3
+*.log
4
+npm-debug.log*
5
+yarn-debug.log*
6
+yarn-error.log*
7
+# Runtime data
8
+pids
9
+*.pid
10
+*.seed
11
+*.pid.lock
12
+.DS_Store
13
+
14
+# Directory for instrumented libs generated by jscoverage/JSCover
15
+lib-cov
16
+
17
+# Coverage directory used by tools like istanbul
18
+coverage
19
+
20
+# nyc test coverage
21
+.nyc_output
22
+
23
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24
+.grunt
25
+
26
+# Bower dependency directory (https://bower.io/)
27
+bower_components
28
+
29
+# node-waf configuration
30
+.lock-wscript
31
+
32
+# Compiled binary addons (http://nodejs.org/api/addons.html)
33
+build/Release
34
+
35
+# Dependency directories
36
+node_modules/
37
+jspm_packages/
38
+
39
+# Typescript v1 declaration files
40
+typings/
41
+
42
+# Optional npm cache directory
43
+.npm
44
+
45
+# Optional eslint cache
46
+.eslintcache
47
+
48
+# Optional REPL history
49
+.node_repl_history
50
+
51
+# Output of 'npm pack'
52
+*.tgz
53
+
54
+# Yarn Integrity file
55
+.yarn-integrity
56
+
57
+# dotenv environment variables file
58
+.env

+ 0
- 0
.npmignore View File


+ 21
- 0
LICENSE View File

@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright (c) 2016 fewieden
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy
6
+of this software and associated documentation files (the "Software"), to deal
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+copies of the Software, and to permit persons to whom the Software is
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in all
13
+copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+SOFTWARE.

+ 71
- 0
MMM-ping.js View File

@@ -0,0 +1,71 @@
1
+/* global Module Log */
2
+
3
+/* Magic Mirror
4
+ * Module: MMM-ping
5
+ *
6
+ * By fewieden https://github.com/fewieden/MMM-ping
7
+ *
8
+ * MIT Licensed.
9
+ */
10
+
11
+Module.register('MMM-ping', {
12
+    defaults: {
13
+        colored: false,
14
+        display: 'both',
15
+        hosts: [],
16
+        updateInterval: 5,
17
+        font: 'medium'
18
+    },
19
+
20
+    start() {
21
+        Log.info(`Starting module: ${this.name}`);
22
+        this.status = {};
23
+        this.checkHosts();
24
+        setInterval(() => {
25
+            this.checkHosts();
26
+        }, this.config.updateInterval * 60000);
27
+    },
28
+
29
+    checkHosts() {
30
+        this.sendSocketNotification('CHECK_HOSTS', this.config.hosts);
31
+    },
32
+
33
+    getDom() {  
34
+        const wrapper = document.createElement('div');
35
+        wrapper.classList.add(this.config.font);
36
+        wrapper.style.textAlign = 'left';
37
+        const hosts = this.config.hosts;
38
+        let domainLength = this.config.hosts.length;
39
+        for (let i = 0; i < domainLength; i++) {
40
+                const isOnline = hosts[i].status;
41
+                console.log(hosts[i].name+' is '+isOnline);
42
+                if ((isOnline && (this.config.display === 'both' || this.config.display === 'online')) ||
43
+                    (!isOnline && (this.config.display === 'both' || this.config.display === 'offline'))) {
44
+                    const div = document.createElement('div');
45
+                    const span = document.createElement('span');
46
+                    span.innerHTML = isOnline ? '<span style=\"color:green;\">ONLINE : </span>' : '<span style=\"color:red;\">OFFLINE : </span>';
47
+                    if (this.config.colored) {
48
+                       span.innerHTML.style.color = isOnline ? 'green' : 'red';
49
+                    }
50
+                    const host = document.createElement('span');
51
+                    host.innerHTML = hosts[i].name;
52
+                    div.appendChild(span);
53
+                    div.appendChild(host);
54
+                    wrapper.appendChild(div);
55
+                }
56
+           }
57
+        return wrapper;
58
+    },
59
+
60
+    socketNotificationReceived(notification, payload) {
61
+        if (notification === 'HOST') {
62
+            for (var i in this.config.hosts) {
63
+                if (this.config.hosts[i].uri == payload.host.uri) {
64
+                        this.config.hosts[i].status = payload.host.status;
65
+                        break; 
66
+                }
67
+            } 
68
+            this.updateDom();
69
+        }
70
+    }
71
+});

+ 50
- 0
README.md View File

@@ -0,0 +1,50 @@
1
+# MMM-ping [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://raw.githubusercontent.com/fewieden/MMM-ping/master/LICENSE) [![Build Status](https://travis-ci.org/fewieden/MMM-ping.svg?branch=master)](https://travis-ci.org/fewieden/MMM-ping) [![Code Climate](https://codeclimate.com/github/fewieden/MMM-ping/badges/gpa.svg?style=flat)](https://codeclimate.com/github/fewieden/MMM-ping) [![Known Vulnerabilities](https://snyk.io/test/github/fewieden/mmm-ping/badge.svg)](https://snyk.io/test/github/fewieden/mmm-ping)
2
+
3
+Ping Module for MagicMirror<sup>2</sup>
4
+
5
+## Example
6
+
7
+![](.github/example.jpg)
8
+
9
+## Dependencies
10
+
11
+* An installation of [MagicMirror<sup>2</sup>](https://github.com/MichMich/MagicMirror)
12
+* npm
13
+* [ping](https://www.npmjs.com/package/ping)
14
+
15
+## Installation
16
+
17
+1. Clone this repo into `~/MagicMirror/modules` directory.
18
+1. Configure your `~/MagicMirror/config/config.js`:
19
+
20
+    ```
21
+    {
22
+        module: 'MMM-ping',
23
+        position: 'bottom_right',
24
+        config: {
25
+            hosts: [
26
+                     {
27
+                        uri: 'github.com',
28
+                        name: 'GitGub'
29
+                    },
30
+                    {
31
+                        uri: '192.168.1.200',
32
+                        name: 'The server'
33
+                    }
34
+            ]
35
+        }
36
+    }
37
+    ```
38
+
39
+1. Run command `npm install --productive` in `~/MagicMirror/modules/MMM-ping` directory.
40
+1. Run command `sudo chmod u+s /bin/ping`.
41
+
42
+## Config Options
43
+
44
+| **Option** | **Default** | **Description** |
45
+| --- | --- | --- |
46
+| `colored` | false | show badge in color or not |
47
+| `display` | 'both' | what should be displayed 'online', 'offline' or 'both' |
48
+| `hosts` | [] | addresses to ping |
49
+| `updateInterval` | 5 | how often the module should ping the hosts in minutes |
50
+| `font` | 'medium' | font size 'xsmall', 'small', 'medium', 'large' or 'xlarge' |

+ 25
- 0
node_helper.js View File

@@ -0,0 +1,25 @@
1
+/* Magic Mirror
2
+ * Module: MMM-ping
3
+ *
4
+ * By fewieden https://github.com/fewieden/MMM-ping
5
+ *
6
+ * MIT Licensed.
7
+ */
8
+
9
+/* eslint-env node */
10
+
11
+const NodeHelper = require('node_helper');
12
+const ping = require('ping');
13
+
14
+module.exports = NodeHelper.create({
15
+    socketNotificationReceived(notification, payload) {
16
+        if (notification === 'CHECK_HOSTS') {
17
+        	payload.forEach((host) => {
18
+            	    ping.sys.probe(host.uri, (isAlive) => {
19
+            	    host.status = isAlive;
20
+                    this.sendSocketNotification('HOST', { host, status: isAlive });
21
+                });
22
+            });
23
+        }
24
+    }
25
+});

+ 32
- 0
package.json View File

@@ -0,0 +1,32 @@
1
+{
2
+  "name": "mmm-ping",
3
+  "version": "1.0.0",
4
+  "description": "Ping Module for MagicMirror2",
5
+  "scripts": {
6
+    "lint": "./node_modules/.bin/eslint ."
7
+  },
8
+  "repository": {
9
+    "type": "git",
10
+    "url": "git+https://github.com/fewieden/MMM-ping.git"
11
+  },
12
+  "keywords": [
13
+    "MagicMirror",
14
+    "ping",
15
+    "online",
16
+    "offline"
17
+  ],
18
+  "author": "fewieden",
19
+  "license": "MIT",
20
+  "bugs": {
21
+    "url": "https://github.com/fewieden/MMM-ping/issues"
22
+  },
23
+  "homepage": "https://github.com/fewieden/MMM-ping#readme",
24
+  "devDependencies": {
25
+    "eslint": "^3.14.1",
26
+    "eslint-config-airbnb-base": "^11.0.1",
27
+    "eslint-plugin-import": "^2.2.0"
28
+  },
29
+  "dependencies": {
30
+    "ping": "^0.1.10"
31
+  }
32
+}

Loading…
Cancel
Save