Bladeren bron

I forgot to port functions from the frontend

Roman Dmitriev 3 maanden geleden
bovenliggende
commit
2ab2e109b5
4 gewijzigde bestanden met toevoegingen van 257 en 46 verwijderingen
  1. 101 26
      html/admin/customers/control-subnets-usage.php
  2. 0 20
      scripts/eyelib/common.pm
  3. 155 0
      scripts/eyelib/database.pm
  4. 1 0
      scripts/fetch_new_arp.pl

+ 101 - 26
html/admin/customers/control-subnets-usage.php

@@ -26,33 +26,108 @@ $zombi_threshold = date('Y-m-d H:i:s', strtotime("-$zombi_days days"));
 	<td><b><?php echo WEB_network_zombi_dhcp; ?><br><?php print "(> ".$zombi_days." ".WEB_days.")"; ?></b></td>
 </tr>
 <?php
-$t_subnets = get_records_SQL($db_link,'SELECT * FROM subnets WHERE office=1 ORDER BY ip_int_start');
+$t_subnets = get_records_SQL($db_link, 'SELECT * FROM subnets WHERE office=1 ORDER BY ip_int_start');
 if (!empty($t_subnets)) {
-foreach ( $t_subnets as $row ) {
-    print "<tr align=center>\n";
-    $cl="data";
-    print "<td class=\"$cl\"><a href=/admin/iplist/index.php?ou=0&cidr=".$row['subnet'].">".$row['subnet']."</a></td>\n";
-    $all_ips = $row['ip_int_stop']-$row['ip_int_start']-3;
-    print "<td class=\"$cl\">".$all_ips."</td>\n";
-#used
-    $used_all = get_count_records($db_link,'user_auth','deleted = 0 AND ip_int >= ? AND ip_int <= ?', [ $row['ip_int_start'], $row['ip_int_stop']]);
-    print "<td class=\"$cl\">".$used_all."</td>\n";
-    $free_all = $all_ips - $used_all;
-    print "<td class=\"$cl\">".$free_all."</td>\n";
-    $dhcp_pool = $row['dhcp_stop']-$row['dhcp_start']+1;
-    print "<td class=\"$cl\">".$dhcp_pool."</td>\n";
-#used pool
-    $used_dhcp = get_count_records($db_link,'user_auth','deleted = 0 AND ip_int >= ? AND ip_int <= ?', [$row['dhcp_start'], $row['dhcp_stop']]);
-    print "<td class=\"$cl\">".$used_dhcp."</td>\n";
-    $free_dhcp = $dhcp_pool - $used_dhcp;
-    print "<td class=\"$cl\">".$free_dhcp."</td>\n";
-    $free_static = $free_all -  $free_dhcp;
-    print "<td class=\"$cl\">".$free_static."</td>\n";
-    $zombi = get_count_records( $db_link, 'user_auth', 'deleted = 0 AND ip_int >= ? AND ip_int <= ? AND last_found <= ?', [ $row['ip_int_start'], $row['ip_int_stop'], $zombi_threshold]);
-    print "<td class=\"$cl\">".$zombi."</td>\n";
-    $zombi = get_count_records( $db_link, 'user_auth', 'deleted = 0 AND ip_int >= ? AND ip_int <= ? AND last_found <= ?', [ $row['dhcp_start'], $row['dhcp_stop'], $zombi_threshold]);
-    print "<td class=\"$cl\">".$zombi."</td>\n";
-    print "</tr>\n";
+    foreach ($t_subnets as $row) {
+        print "<tr align=center>\n";
+        $cl = "data";
+
+        // Subnet
+        print "<td class=\"$cl\"><a href=/admin/iplist/index.php?ou=0&cidr=" . htmlspecialchars($row['subnet']) . ">" . htmlspecialchars($row['subnet']) . "</a></td>\n";
+
+        // Total IPs
+        $subnet_ips = max(0, $row['ip_int_stop'] - $row['ip_int_start'] + 1);
+        if ($subnet_ips > 4 ) { $all_ips = $subnet_ips - 3; } else { $all_ips = $subnet_ips; }
+        print "<td class=\"$cl\">" . $all_ips . "</td>\n";
+
+        // Used (total)
+        $used_all = get_count_records($db_link, 'user_auth', 'deleted = 0 AND ip_int >= ? AND ip_int <= ?', [$row['ip_int_start'], $row['ip_int_stop']]);
+        $total_used_percent = ($all_ips > 0) ? ($used_all / $all_ips) * 100 : 0;
+        $total_class = '';
+        if ($total_used_percent >= 95) {
+            $total_class = 'error';
+        } elseif ($total_used_percent >= 85) {
+            $total_class = 'warn';
+        }
+        print "<td class=\"" . ($total_class ?: $cl) . "\">" . $used_all . "</td>\n";
+
+        // Free (total)
+        $free_all = max(0, $all_ips - $used_all);
+
+        $free_total_class = '';
+        if ($all_ips > 0) {
+            $free_total_percent = ($free_all / $all_ips) * 100;
+            if ($free_total_percent <= 5) {
+                $free_total_class = 'error';
+            } elseif ($free_total_percent <= 15) {
+                $free_total_class = 'warn';
+            }
+        }
+        print "<td class=\"" . ($free_total_class ?: $cl) . "\">" . $free_all . "</td>\n";
+
+        // DHCP pool size
+        $dhcp_pool = max(0, $row['dhcp_stop'] - $row['dhcp_start'] + 1);
+        print "<td class=\"$cl\">" . $dhcp_pool . "</td>\n";
+
+        // Used (DHCP)
+        $used_dhcp = 0;
+        if ($dhcp_pool > 0) {
+            $used_dhcp = get_count_records($db_link, 'user_auth', 'deleted = 0 AND ip_int >= ? AND ip_int <= ?', [$row['dhcp_start'], $row['dhcp_stop']]);
+        }
+        $dhcp_used_percent = ($dhcp_pool > 0) ? ($used_dhcp / $dhcp_pool) * 100 : 0;
+        $dhcp_used_class = '';
+        if ($dhcp_used_percent >= 95) {
+            $dhcp_used_class = 'error';
+        } elseif ($dhcp_used_percent >= 85) {
+            $dhcp_used_class = 'warn';
+        }
+        print "<td class=\"" . ($dhcp_used_class ?: $cl) . "\">" . $used_dhcp . "</td>\n";
+
+        // Free (DHCP)
+        $free_dhcp = max(0, $dhcp_pool - $used_dhcp);
+        $free_dhcp_class = '';
+
+        if ($dhcp_pool > 0) {
+            $free_dhcp_percent = ($free_dhcp / $dhcp_pool) * 100;
+            if ($free_dhcp_percent <= 5) {
+                $free_dhcp_class = 'error';
+            } elseif ($free_dhcp_percent <= 15) {
+                $free_dhcp_class = 'warn';
+            }
+        }
+        print "<td class=\"" . ($free_dhcp_class ?: $cl) . "\">" . $free_dhcp . "</td>\n";
+
+        // Free static
+        $free_static = max ( 0, $free_all - $free_dhcp);
+        print "<td class=\"$cl\">" . $free_static . "</td>\n";
+
+        // Zombie (total) — от общего размера подсети
+        $zombi_total = get_count_records($db_link, 'user_auth', 'deleted = 0 AND ip_int >= ? AND ip_int <= ? AND last_found <= ?', [$row['ip_int_start'], $row['ip_int_stop'], $zombi_threshold]);
+        $zombi_total_class = '';
+        if ($all_ips > 0) {
+            $zombi_total_ratio = ($zombi_total / $all_ips) * 100;
+            if ($zombi_total_ratio >= 30) {
+                $zombi_total_class = 'error';
+            } elseif ($zombi_total_ratio >= 20) {
+                $zombi_total_class = 'warn';
+            }
+        }
+        print "<td class=\"" . ($zombi_total_class ?: $cl) . "\">" . $zombi_total . "</td>\n";
+
+        // Zombie (DHCP) — от размера DHCP-пула
+        $zombi_dhcp = get_count_records($db_link, 'user_auth', 'deleted = 0 AND ip_int >= ? AND ip_int <= ? AND last_found <= ?', [$row['dhcp_start'], $row['dhcp_stop'], $zombi_threshold]);
+        $zombi_dhcp_class = '';
+        if ($dhcp_pool > 0) {
+            $zombi_dhcp_ratio = ($zombi_dhcp / $dhcp_pool) * 100;
+            if ($zombi_dhcp_ratio >= 30) {
+                $zombi_dhcp_class = 'error';
+            } elseif ($zombi_dhcp_ratio >= 20) {
+                $zombi_dhcp_class = 'warn';
+            }
+        }
+        print "<td class=\"" . ($zombi_dhcp_class ?: $cl) . "\">" . $zombi_dhcp . "</td>\n";
+
+        print "</tr>\n";
     }
 }
 ?>

+ 0 - 20
scripts/eyelib/common.pm

@@ -54,7 +54,6 @@ GetUnixTimeByStr
 is_ad_computer
 is_default_ou
 is_dynamic_ou
-is_hotspot
 new_auth
 new_user
 process_dhcp_request
@@ -275,25 +274,6 @@ sub delete_device {
 
 #---------------------------------------------------------------------------------------------------------------
 
-sub is_hotspot {
-    my ($db, $ip) = @_;
-    return 0 unless $db && defined $ip;
-
-    my @subnets = get_records_sql(
-        $db,
-        "SELECT subnet FROM subnets WHERE hotspot = 1 AND LENGTH(subnet) > 0"
-    );
-
-    my $pat = Net::Patricia->new;
-    for my $row (@subnets) {
-        $pat->add_string($row->{subnet}) if defined $row->{subnet};
-    }
-
-    return $pat->match_string($ip) ? 1 : 0;
-}
-
-#---------------------------------------------------------------------------------------------------------------
-
 sub get_office_subnet {
     my ($db, $ip) = @_;
     return undef unless $db && defined $ip;

+ 155 - 0
scripts/eyelib/database.pm

@@ -43,6 +43,19 @@ use Text::CSV;
 our @ISA = qw(Exporter);
 
 our @EXPORT = qw(
+is_hotspot
+get_queue
+get_group
+get_subnet_description
+get_filter_instance_description
+get_vendor_name
+get_ou
+get_device_name
+get_device_model
+get_device_model_name
+get_building
+get_filter
+get_login
 StrToIp
 IpToStr
 prepare_audit_message
@@ -135,6 +148,148 @@ our %db_schema;
 
 #---------------------------------------------------------------------------------------------------------------
 
+sub is_hotspot {
+    my ($db, $ip) = @_;
+    return 0 unless $db && defined $ip;
+
+    my @subnets = get_records_sql(
+        $db,
+        "SELECT subnet FROM subnets WHERE hotspot = 1 AND LENGTH(subnet) > 0"
+    );
+
+    my $pat = Net::Patricia->new;
+    for my $row (@subnets) {
+        $pat->add_string($row->{subnet}) if defined $row->{subnet};
+    }
+
+    return $pat->match_string($ip) ? 1 : 0;
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+# Вспомогательная функция для проверки "пустого" значения
+sub _is_empty {
+    my ($val) = @_;
+    return !defined $val || $val eq '';
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_queue {
+    my ($dbh, $queue_value) = @_;
+    return '' if _is_empty($queue_value);
+    my $queue = get_record_sql($dbh, "SELECT queue_name FROM queue_list WHERE id = ?", $queue_value);
+    return $queue->{queue_name} // '';
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_group {
+    my ($dbh, $group_id) = @_;
+    return '' if _is_empty($group_id);
+    my $group = get_record_sql($dbh, "SELECT group_name FROM group_list WHERE id = ?", $group_id);
+    return $group->{group_name} // '';
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_subnet_description {
+    my ($dbh, $subnet_id) = @_;
+    return '' if _is_empty($subnet_id);
+    my $subnet = get_record_sql($dbh, "SELECT * FROM subnets WHERE id = ?", $subnet_id);
+    return '' unless $subnet;
+    my $desc = $subnet->{description} // '';
+    return "$subnet->{subnet}&nbsp;($desc)";
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_filter_instance_description {
+    my ($dbh, $instance_id) = @_;
+    return '' if _is_empty($instance_id);
+    my $instance = get_record_sql($dbh, "SELECT * FROM filter_instances WHERE id = ?", $instance_id);
+    return '' unless $instance;
+    my $desc = $instance->{description} // '';
+    return "$instance->{name}&nbsp;($desc)";
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_vendor_name {
+    my ($dbh, $v_id) = @_;
+    return '' if _is_empty($v_id);
+    my $vendor = get_record_sql($dbh, "SELECT name FROM vendors WHERE id = ?", $v_id);
+    return $vendor->{name} // '';
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_ou {
+    my ($dbh, $ou_value) = @_;
+    return undef if _is_empty($ou_value);
+    my $ou_name = get_record_sql($dbh, "SELECT ou_name FROM ou WHERE id = ?", $ou_value);
+    return $ou_name ? $ou_name->{ou_name} : undef;
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_device_name {
+    my ($dbh, $device_id) = @_;
+    return undef if _is_empty($device_id);
+    my $dev = get_record_sql($dbh, "SELECT device_name FROM devices WHERE id = ?", $device_id);
+    return $dev ? $dev->{device_name} : undef;
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_device_model {
+    my ($dbh, $model_value) = @_;
+    return undef if _is_empty($model_value);
+    my $model_name = get_record_sql($dbh, "SELECT model_name FROM device_models WHERE id = ?", $model_value);
+    return $model_name ? $model_name->{model_name} : undef;
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_device_model_name {
+    my ($dbh, $model_value) = @_;
+    return '' if _is_empty($model_value);
+    my $row = get_record_sql($dbh, "SELECT M.id, M.model_name, V.name FROM device_models M, vendors V  WHERE M.vendor_id = V.id AND M.id = ?", $model_value);
+    return '' unless $row;
+    my $vendor = $row->{name} // '';
+    my $model = $row->{model_name} // '';
+    return "$vendor $model";
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_building {
+    my ($dbh, $building_value) = @_;
+    return undef if _is_empty($building_value);
+    my $building_name = get_record_sql($dbh, "SELECT name FROM building WHERE id = ?", $building_value);
+    return $building_name ? $building_name->{name} : undef;
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_filter {
+    my ($dbh, $filter_value) = @_;
+    return '' if _is_empty($filter_value);
+    my $filter = get_record_sql($dbh, "SELECT name FROM filter_list WHERE id = ?", $filter_value);
+    return $filter->{name} // '';
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub get_login {
+    my ($dbh, $user_id) = @_;
+    return '' if _is_empty($user_id);
+    my $login = get_record_sql($dbh, "SELECT login FROM user_list WHERE id = ?", $user_id);
+    return $login->{login} // '';
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
 sub prepare_audit_message {
     my ($dbh, $table, $old_data, $new_data, $record_id, $operation) = @_;
 

+ 1 - 0
scripts/fetch_new_arp.pl

@@ -259,6 +259,7 @@ $auth_sql = "SELECT id, mac FROM user_auth WHERE mac IS NOT NULL AND deleted = 0
 my @auth_full_list = get_records_sql($dbh, $auth_sql);
 foreach my $auth (@auth_full_list) {
     next if (!$auth);
+    next if (!$auth->{mac});
     my $auth_mac = mac_simplify($auth->{mac});
     next if (exists $auth_table{full_table}{$auth_mac});
     $auth_table{full_table}{$auth_mac} = $auth->{id};