Nair18 / auto-increment-alert

Script to send an alert for DB row out of index. ✍️ MIT LICENSE.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What the heck is this?

This script contains the code to identify those tables in database that are having auto-increment ids and are running short off those ids. Once it identifies those tables it will send an alert to slack using the webhook through the script. Lets visualize this scenario through the screenshots.

Build up

This pic below shows that currently I have two tables in my database, table T1 and table T2(Ignore the entries in the table, sorry if you think its offensive :) ). Each table has auto-increment id. In table T1 the auto-increment id starts by default from index 1. while in table T2 the auto-increment id starts from index 100000000. Both the tables have sufficient ids because the max value of the int(11) is 2147483647. Here int(11) is the data type of the primary keys in both tables.

pic2

What happens when the script runs?

When you run the python script, following messages will appear on the slack #general channel screeen. We can send the alert to any channel, accordingly the url will change. Here you can see that message reads --> "hello, everything is fine", that means everything is fine and ids are not running short of indexes. You see so many messages of same type because I used a timer due to which it sends the update to slack every 5 mins.

pic1

Recreating the scenario for testing

Now in next step here's what I do, I will change the auto-increment value of table T2 to some very big number such that the number is >2147483647(max limit of auto-increment id). I did this opertion using alter table command.

pic3

Consequences

Now when you run python script, see what happens. Look at the last message in the pic below. It reads --> "table T2 are about to run short of auto-increment id".

pic4

Below is the query I used, to identify whether there is any table in database that is running short of auto-incement ids.

pic5

Above query gives a beautiful table that contains the auto-increment ratio of each table in database. If this ratio is around 0.954, that means about 95% of the ids are used up and are about to run short of ids.

pic6

Formula to calculate auto-increment ratio

pic7

About

Script to send an alert for DB row out of index. ✍️ MIT LICENSE.

License:MIT License


Languages

Language:Python 100.0%