mysql alerting via email for value falling below specified amount - mysql

I am running a simple mysql db that contatins values for temperature sensors in a clients home. I have been looking into different ways to create email alerting if the temp is below or above a preset value.
There are ways that I can accomplish this via a simple php script to be ran off of a cron job from what i have gathered but the problem with them is it needs to be put 'on hold' for a certain amount of time that way once a client gets an alert they wont keep getting that alert every five minutes when the cron runs.
Also as a future question of this project, is there a way to have cron jobs created from values that users store in the sql db? That way the users can have customized alerting?
Sorry if it didn't come out clear, I will be right next to the computer all day to answer questions on this regard if need be.

To resolve your problem, create an alerts table that keeps track of when the last alert was. Depending on your platform, it may be possible for PHP to schedule cron jobs. You could save the user settings in a table and have PHP create/update/delete the cron jobs.

Related

Auto send text message on MySQL

I have been asked to send text message(sms) automatically when there is data changed in a table on mysql.. I have been searched many times and it comes with trigger..
I was wondering how mysql will send the text message(sms).. is it using gammu or somethingelse?
Thank you anyway for the response..
MySQL doesn't have any builtin functionality to send SMS messages.
As your search revealed, we can use a TRIGGER to perform actions whenever a row is inserted, updated or deleted from a table. One of the possible actions the trigger might perform is to INSERT a row to another table.
A separate component (not the MySQL database) could connect to the database and periodically poll that other table, and send SMS messages.
You have a few choices here, none of them wonderful.
One is to write a program in some application language (php or java, perhaps) to poll the dbms looking for changes in a table. Then it can send the requisite messages. You can do this by putting a time-updated timestamp in each row of the table. Then your polling will query for rows changed since the last time the polling ran. If you run this query once every six seconds, for example, you'll be no more than a tenth of a minute out of date with these SMS messages. That probably works for most wireless-network SMS.
Another is to add a user-defined function to MySQL and then invoke that function from a trigger. This one, for example, sends a message to a message queuing system like RabbitMQ or ActiveMQ. https://github.com/mysqludf/lib_mysqludf_stomp
You could then write a queue consumer app to send your SMS messages. This approach has low latency and will scale up nicely. But, it requires extending MySQL, and rigging up a nontrivial queuing system.
A third approach is to get the software that updates your table also to send the SMS messages.

Notify Users When Record Hasn't Been Updated in X Amount of Time

I'm working on a Rails 3.2 application where users create, read and update multiple kinds of reports. We use MySQL and Redis.
I would like to notify users when one of their reports hasn't been updated in the previous X months by showing them notifications on their profile/dashboard page in the browser.
I would prefer to do this asynchronously.
At some point, I would also like to have live, in-app notifications so users can be notified when a report they're watching has been updated or someone likes their report. Conceptually, I was contemplating the best way of going about doing this...
A cron job that runs a SQL query and retrieves all reports that have update_at fields older than 3 months, and creates a notification record linked to that user and report. A new notification record will be created only if user didn't have an existing notification.
A background job, using something like Resque that checks the database periodically throughout the day. Notifications are stored in a queue. This seems like it would scale better into a more robust, in-app notifications feature.
Are these my only two options? Is there a better asynchronous way to listen to the database, and notify a user when one of their records hasn't been updated in 3 months? Is some implementation of websockets necessary here?
I don't believe you necessarily need to use CPU cycles with some always-running background job, but that depends on how powerful you want these notifications to be. If you want them to be real-time, and you plan on using a publisher-subscriber model in the future then I'd consider a redis + node server setup.
The complexity of websockets means more implementation time. Will that extra time spent really benefit you? If it's very important that your notifications be asynchronous and real-time, like exchanging messages in a chat app or getting pop-up notifications on Facebook, then this is the right choice.
However, if all you really want to do is let users know when their reports are past a certain age, when they request their profile page, a cron that runs every couple of hours will be much easier to implement. You could even use Google Analytics to get a feel for when users are putting the heaviest burden on your app, and schedule the job around that.
First, create a rake task that completes the behavior your described above--check for old reports and create notifications. Put that in your app's lib/tasks directory. And then write a cron job like so:
$ 0 6,12,18 * * * cd /path/to/app && bundle exec rake task_name
This task will run 3x a day, at 6am, 12pm and 6pm.
From there, if you want to create an activity feed for your users, you could also implement a synchronous pub-sub solution pretty simply. E.g., User A subscribes to User B's reports, User B publishes a new report, after_save callback to create a notification for User A. This will be slower, it won't be live, but if it's a small app and this feature isn't a core part, then this makes better use of your time than the other options.

Design a job scheduler for subscription system

I have to develop/use a scheduler in my NodeJS project to check if any subscriptions need to be renewed. Job needs to run once every day and check my database if 'enddate' of a any user subscription record falls on that day. If it is, run a piece of logic to renew it (i have my own renew logic which doesnt depend on any external services like payment providers/gateways).
Application will be in cloud which means all my app instances have to be stateless. Database (MySQL) is the only stateful part of the system and Renew logic has to run only once.
How can i do this ?
My database table row for a subscription looks like this:
Id: 1, User: John Doe, Plan: Monthly Premium, Start Date: 1-10-2015, End Date: 30-10-2014, status : "ACTIVE"
Problems:
1.) Trigger job running logic everyday - Easy to solve in nodejs with setInterval or a scheduler library
2.) Running a worker or a piece of code that checks the db and renews it - This needs to happen only once per user subscription. Many instances can potentially run the renew logic at the same time when daily scheduler triggers the process. Not sure how to solve this part of the problem.
One possible solution consists of a shell script, put in your crontab, that would run each day.
This script would do :
Using a sql statement or stored procedure, find records where subscription
end date is today
For those records, apply your logic. (send email or anything else)
Flag the record to indicate that a something has been done
for this user (and put in another field the process date).
you can create a worker and use node-cron to trigger subscription expire everyday.
i'm not sure what renew process does. you can use lock something until the renew process complete and release it. I was thinking i would use redlock
You can use Agenda, but it requires a MongoDb to maintain its state. If you are okay with adding one more DB, then you can give this a shot.

Best way to report events / read events (also MySQL)

So I'm going to attempt to create a basic monitoring tool in VB.net. Now I'd like some advice on how basically to tackle the logging and reporting side of things so I'd appreciate some responses from users who I'm sure have a better idea than me and can tell me far more efficient ways of doing things.
So my plan is to have a client tool, which will read from a MySQL database values and basically change every x interval, I'm thinking 10/15 minutes at the moment. This side of the application is quite easy, I mean I can get something to read a database every x amount of time and then change labels and display alerts based on them. - This is all well documented and I am probably okay with that.
The second part is to have a client that sits in the system tray of the server gathering the required information. Now the system tray part I think will probably be the trickiest bit of this, however that's not really part of my question.
So I assume I can use the normal information gathering commands and store them perhaps as strings and I can then connect to the same database and add them to the relevant fields. For example if I had a MySQL table called "server" and a column titled "Connection" I could check if the server has an internet connection for example and store the result as the value 1 for yes and 0 for no and then send a MySQL command to the table to update the "connection" value to either 0/1.
Then I assume the monitoring tool I can run a MySQL query to check the "Connection" column and if the value is = 0 change a label or flag an error and if 1 report that connectivity is okay?
My main questions about the above are listed below.
Is using a MySQL database the most efficient way of doing something like this?
Obviously if my database goes down there's no more reporting, I still think that's a con I'll have to live with though.
Storing everything as values within the code is the best way to store my data?
Is there anything particular type of format I should use in the MySQL colum, I was thinking maybe tinyint(9)?
Is the above method redundant and pointless?
I assume all these database connections could cause some unwanted server load, however the 15 minute refresh time should combat that.
Is there a way to properly combat delays with perhaps client updating not in time for the reporter so it picks up false data, perhaps a fail safe for a column containing last updated time?
You probably don't need the tool that gathers information per se. The web app (real time monitor) can do that, since the clients are storing their information in the same database. The web app can access the database every 15 minutes and display the data, without the intermediate step of saving it again. This will provide the web app with the latest information instead of a potential 29-minute delay.
In other words, the clients are saving the connection information once. Don't duplicate it in the database.
MySQL should work just about as well as anything.
It's a bad idea to hard code "everything". You can use application settings or a MySQL table if you need to store IPs, etc.
In an application like this, the conversion will more than offset the data savings of a tinyint. I would use the most convenient data type.

How to create an auto task schedule ios notification based on an event with mysql data

I have a problem related to automatic task scheduling.
Currently i am able to find out when my customer has last credited his account, how am i able to find out whether he will pay anything in the next 3 days?
So if no payment has been made in the next three days for any customer, to automatically alert me preferably by a notification directly to my ipad.
I dont want myself to open the app for checks to be done only when i log in, because then if i jump on my application 6 days later, i could have had a customer that hasnt paid in 6 days when the app should have alerted me on the 3rd day so i could ring my customer up to deal with the matter.
I need to work in this matter due to the structure of my application and business.
I am able to monitor everything else but need some insight on how I can go about doing this. the current notification system inside the phone only fires based on time, and I cannot do interval checks where maybe i could run a background task, if that would work then i would have done it like that but thats not the case.
Pavan
If I understand your question correctly, you should compute the interval of the event that you want and post a wake-up timer that is that period of time from "now." If you need it through the notification center, then just handle it silently and clear it from the notifications.
Based on the discussion below:
You will need a little bit of server work. APNS looks complicated, but it really has very few moving parts -- especially if it is a private App. What system component is keeping an eye on Amazon? Do you have an App or web server? For example, if I were to poke a record into your system (purchased services) what workflow is triggered to notify Accounting to process an invoice and collections at a later date? Am I making any sense of your system architecture?
Perfect - you are done. You have all the system components you need and the rest is coding. The server app processes the accounts DB and finds new entries. If found, it publishes a record ID to the APNS server (Apple owns this server). You write code to register to receive the push-notification (subscriber). When you get a push, that will wake up your registered app with the record ID (and some other subscription stuff for bookkeeping -- but you are the only subscriber and only subscribing to one DB table -- so you can largely ignore. Now turn around and query based on that record. Done!