-1

I want to be able to perform an action on power failure i.e when electricity goes off since my network routers are on UPS so it doesn't know about power failures so somehow I want to be able to store the power failure data into a text file through php so anyway possible to write power-failure data to a text file through PHP??

Here is a screenshot of Mikrotik with different windows open regarding the question : enter image description here

Umair Shah
  • 2,305
  • 2
  • 25
  • 50
  • Explain your question in brief with example – M A SIDDIQUI Feb 15 '17 at 10:42
  • @MASIDDIQUI : updated :D – Umair Shah Feb 15 '17 at 10:44
  • 1. How is your router suppose to get power on power failure? 2. How do you propose to detect a power failure? 3. Does Mikrotik Router support remote control with API / SSH / console? – Koala Yeung Feb 15 '17 at 10:45
  • @KoalaYeung : Yes..Mikrotik Router has it's own Terminal..It supports API,SSH,Console all of it..! – Umair Shah Feb 15 '17 at 10:46
  • What is doing the power failure detection? Router, UPS, some other device? – Auris Feb 15 '17 at 10:47
  • If there were a power failure, is there anyway to detect in your setup? (e.g. some machine would go offline so you cannot ping it) – Koala Yeung Feb 15 '17 at 10:47
  • What UPS do you have? – Kitson88 Feb 15 '17 at 10:48
  • @KoalaYeung : Yes..I want to know that how I can detect a power failure and then write data to a text file through PHP or any other way through anything but to write data to a text file so that I can read that text file through php and then perform actions in the Mikrotik Router with the help fo it's PHP API..! – Umair Shah Feb 15 '17 at 10:48
  • @Kitson88 : The UPS..It's not really branded..It's home made...! – Umair Shah Feb 15 '17 at 10:49
  • 1
    @UmairShahYousafzai Some expansive UPS already support power failure alerts. But if you don't have those, you'd probably need to buy some extra sensors (e.g. [like this](https://serverscheck.com/sensors/sensor_power.asp)). You can plug this one specifically outside UPS to detect the failure. – Koala Yeung Feb 15 '17 at 10:50
  • @KoalaYeung : Yes..I also thought about that first that a device connected to the normal electricity other than UPS would go off on power-failure but since all of my devices are connected to UPS power so there is no device for now which can go off on power-failure so that I can do actions based on that..So any other way??? – Umair Shah Feb 15 '17 at 10:50
  • 1
    Option 1. Have a web app that pings any of the devices that may go out during power faulure (simple CURL ping). If Ping not available you have either power outage or network outage. If detected simply write to data to file. Have your router check that file on regular basis and perform required action based on that. Problem is if al ldevices are on UPS. In that case power failure wont break ping as devices will still be online. You would need something that can detect the failure and notify - new piece of hardware – Auris Feb 15 '17 at 10:51
  • 1
    Is your whole network on UPS if yes which model.? Usually they write to a log the power loss if they are connected via USB to a server (some may support linux most are for Windows). Can you access that software's log? –  Feb 15 '17 at 10:53
  • @UmairShahYousafzai: Also you'd need a long running process to keep monitoring the sensor's broadcast (e.g. SNMP). PHP is not a good choice to do such job. You'd probably need [Golang](https://golang.org/), [Python](https://www.python.org/) or any language that works well running as daemon. – Koala Yeung Feb 15 '17 at 10:54
  • Mikrotik is really way advance Router..it has it's own scripting language and terminal to execute all possible commands which includes pinging etc and yes logs are there but since I said that on power-failure my devices don't go off since all of them are connected to the UPS so that's why..! – Umair Shah Feb 15 '17 at 10:55
  • @KoalaYeung : Your options seems much better but infact...if you were to say that If I need a cheap solution so an extra device wouldn't be hard to come by...to connect that to the normal electricity other than the UPS so that it goes off on power-failure so that I can detect that through terminal by pinging and then perform actions based on that...! – Umair Shah Feb 15 '17 at 10:58
  • @UmairShahYousafzai is your UPS connected to a pc or server? Is all your network under your UPS? Does the UPS provide 15 minutes power supply on power failure to all devices on the network? Do you have done the mistake to add a printer to the devices that the UPS provide power? Please provide more information there are many possible ways to do that but in this case you need to provide more info. this is hardware based question and it does not involve the router only. –  Feb 15 '17 at 10:59
  • @PeterDarmis : Yes..You can say that Mikrotik is a Server and it's connnected to the UPS and yes all of my network is connected to my UPS...The UPS provides more than 15 minutes backup as the devices are all DC based so they do no consume too much power so that's why can stay live for quite longer...And No all the devices are simply routers so I haven't added any printer to those devices..! – Umair Shah Feb 15 '17 at 11:04
  • @UmairShahYousafzai Then maybe plug a RPi to power source outside of UPS for detection? – Koala Yeung Feb 15 '17 at 11:05
  • Thou the restart time can be longer than the power outage. – Koala Yeung Feb 15 '17 at 11:06
  • @KoalaYeung : The power outage can remain for 1+ hour so restart time can't be more than power-outage..! :D – Umair Shah Feb 15 '17 at 11:08
  • If Mikrotik can do all the pinging and detection, store he event log, reprogram rules on its own and also host php program. Then all you need to do afterwards is program php to read the event log beautifully. End of story. – Koala Yeung Feb 15 '17 at 11:09
  • @UmairShahYousafzai so a UPS that provides power to all routers.Can you connect the UPS to a small PC? I mean what model is the UPS does it have a software? They usually do and even if they don't there is open source software that can be used on several UPS cases. Answer me this and i can explain you better my answer. –  Feb 15 '17 at 11:09
  • @PeterDarmis updated the question..Please take a look at the screenshot..! – Umair Shah Feb 15 '17 at 11:12
  • @KoalaYeung updated the question..Please take a look at the screenshot..! – Umair Shah Feb 15 '17 at 11:12
  • @Auris updated the question..Please take a look at the screenshot..! – Umair Shah Feb 15 '17 at 11:12
  • @UmairShahYousafzai this is a screenshot of the mikrotik router interface. Do you have the model of the UPS? Is it an APC UPS for example? –  Feb 15 '17 at 11:15
  • @PeterDarmis : Yes..It's the screenshot of Mikrotik Interface with just few windows opened...The UPS is home made...out of Inverter+Battery+Converter separately..! So I am afraid it doesn't have any advance circuit to do anything more than that..! The Mikrotik Model is RB750GL here link : https://routerboard.com/rb750gl – Umair Shah Feb 15 '17 at 11:19
  • It seems that Mikrotik Winbox runs on customized Linux (RouterOS). But you cannot run PHP on it. You'll need another machine for your web interface and storage. You'd better off do the detection on this device instead of the router. – Koala Yeung Feb 15 '17 at 11:20
  • @KoalaYeung : Yes....Mikrotik can't run PHP directly but it has PHP API through which I can do actions remotely on a hosting server too..As my Mikrotik is remotely accessable through : http://network.huntedhunter.com:81 – Umair Shah Feb 15 '17 at 11:23
  • Its not a good idea to publish username / password with the URL to your router... – Koala Yeung Feb 15 '17 at 11:25
  • @KoalaYeung : Here is the `RouterOS` PHP API : https://github.com/BenMenking/routeros-api – Umair Shah Feb 15 '17 at 11:26
  • @KoalaYeung : yes...the user is just read based not with full rights..so you can just read everything..! – Umair Shah Feb 15 '17 at 11:26
  • 1
    @UmairShahYousafzai you will need something to sensor mains so check out arduino for that. You will need a pocket machine to run a small linux web server get a RPi if you wish. You will need to write something or find something from an arduino programming example to detect power changes and send to RPi via a network card. Write your own PHP application then to manage power loss or change detection. Wrap this in your home-made UPS and there you are you have just upgraded your UPS. I would do that if i was in your case. –  Feb 15 '17 at 11:28
  • 1
    @PeterDarmis : Yes...I also thought of that at first to workout with Arduino and Raspberry Pi..That's also a good solution even for more complex events..! And Thanks for this...It's a very good solution..I was always thinking about to checkout the Arduino & Raspberry Pi boards..! – Umair Shah Feb 15 '17 at 11:29
  • @UmairShahYousafzai something like this could transform the UPS to a web based managed UPS. –  Feb 15 '17 at 11:31
  • @PeterDarmis : Can you post this one as answer so that I can accept it for answer with the other possiblities which you mentioned in your past comments? – Umair Shah Feb 15 '17 at 11:33

2 Answers2

1

There is no proper way of doing this.
First of, you need an external machine.
Why? Because if a machine shuts down because it has no power, it cannot execute code anymore !

So here is what I thought:
If you have a server running on the routers that can be accessed remotly, you could simply "ping" ("file_get_contents" or "curl") your router with requests. If you get an error, that means it's not online anymore. And then store this into a text file.

If you want to use a local laptop to do this, you could just simply "ping" your router location (could be 192.168.0.1, depends on your router) and if it doesn't respond, that means it's offline.

Ad5001
  • 748
  • 5
  • 19
  • UPS devices provide power on power network failure for at least 15 minutes. This means that you will still have 15 minutes time provided by the UPS batteries to execute something. –  Feb 15 '17 at 11:05
  • Yes, but if your UPS is providing power to router, you will not detect the power outage until UPS runs out of charge, at that point it is too late as all other devices are also offline. The smartest way to do this is to actually get a propper router with PO detection and network access. This router can connect to external or internal API endpoint and execute whatever you need to execute. Why reinvent a bicycle with additional PO monitoring hardware and laptops? – Auris Feb 22 '17 at 15:08
1

Most UPS devices have SNMP server where you can query simple statistics (definitely Power failures).

There are some linux packages for instance nut-snmp, which you could use to get information from the UPS.

Arash
  • 400
  • 4
  • 11