-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipblock80-cron.php
120 lines (103 loc) · 3.98 KB
/
ipblock80-cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* This script is intended to be run as a cron-job on a solusvm master. It will run IP blocks up to a certain percentage threshold at which point it will re-prioritize the blocks.
*
* @author Jonathan Wright
* @website https://www.knownhost.com
* @email [email protected]
* @version 1.0.1
*
*/
// start config
// Default blank priority to be used. This should probably be 0. Blocks above the threshold are reset to this.
define('DEFAULT_PRIORITY',0);
// priority to be assigned for blocks that should be used first. Solus pulls first from the block with the highest numbered priority.
define('HIGH_PRIORITY',100);
// Set the percent used threshold that the script should let IP blocks go to. After reaching this threshold their priority is reset to 0 unless they drop back below the threshold.
define('PERCENT_THRESHOLD',82);
// Should reserved IPs be counted as used
define('COUNT_RESERVED_AS_USED',true);
// Use dynamic block balancing to attempt to get blocks to equal usage and maintain it?
define('DYNAMIC_BALANCING',true);
// end config
// do not modify below this line
// lets parse solus's config file so we know we have proper db connection info
$solusinfo = fread(fopen('/usr/local/solusvm/includes/solusvm.conf',"r"),filesize('/usr/local/solusvm/includes/solusvm.conf'));
$solusinfo = explode(":",$solusinfo);
// If we didn't get a 4-item array from the above info we cannot connect to mysql
if(count($solusinfo) != 5)
die('Unable to fetch SolusVM database connection info from SolusVM config file.');
// convert into named vars for readability
$solusdbname = $solusinfo[0];
$solusdbuser = $solusinfo[1];
$solusdbpass = $solusinfo[2];
$solusdbhost = $solusinfo[3];
$db = new mysqli($solusdbhost, $solusdbuser, $solusdbpass, $solusdbname);
if($db->connect_errno > 0){
die('Unable to connect to SolusVM database [' . $db->connect_error . ']' . "\n");
}
/**
* Fetches the blockid and name of all IP blocks
* @return array blockid=>name
*/
function get_ipblocks($db){
$sql = "select `blockid`,`name` from ipblocks";
$result = $db->query($sql);
$ipblocks = array();
while($row = $result->fetch_assoc()){
$ipblocks[$row['blockid']] = $row['name'];
}
return $ipblocks;
}
/**
* Gets the percentage used of a block
* @param $db
* @param $blockid
* @return int percentage of block used
*/
function block_used_percent($db,$blockid){
// Lets find out how many IPs are in the block. Not everyone has full /24s.
$sql = "select count(`ipaddressid`) as count from `ipaddresses` where `blockid`='$blockid'";
$result = $db->query($sql)->fetch_assoc();
$total_ips = $result['count'];
$sql = null;
if(COUNT_RESERVED_AS_USED == true) {
$sql = "select count(`ipaddressid`) as `count` from `ipaddresses` where `blockid`='$blockid' and (`reserved` = 1 or `vserverid`!=0)";
}else{
$sql = "select count(`ipaddressid`) as `count` from `ipaddresses` where `blockid`='$blockid' and `vserverid`!=0";
}
$result = $db->query($sql)->fetch_assoc();
$used_ips = $result['count'];
return round(($used_ips / $total_ips)*100,1);
}
/**
* Simply checks of the used percent is greater than the threshold so we can know how to handle the block
* @param $used_percent
* @return bool true if above threshold, false if below
*/
function threshold_check($used_percent){
if($used_percent > PERCENT_THRESHOLD)
return true;
return false;
}
/**
* Updates the priority set on the IP block
* @param $db
* @param $blockid
* @param $deploy_order
*/
function set_block_priority($db,$blockid,$deploy_order,$used_percent){
if(DYNAMIC_BALANCING)
$deploy_order = 100 - $used_percent;
$sql = "update `ipblocks` set `deploy_order`='$deploy_order' where `blockid`='$blockid'";
$db->query($sql);
}
$ipblocks = get_ipblocks($db);
foreach($ipblocks as $blockid=>$name){
$used_percent = block_used_percent($db,$blockid);
if(threshold_check($used_percent)){
set_block_priority($db,$blockid,DEFAULT_PRIORITY,$used_percent);
}else{
set_block_priority($db,$blockid,HIGH_PRIORITY,$used_percent);
}
}