**First, the Detailed Explanation of SNMP Programming**
SNMP (Simple Network Management Protocol) is a remote monitoring application that operates over TCP/UDP. It is used to monitor various aspects of networked hosts, such as memory usage, CPU utilization, and disk space. SNMP is typically divided into two main components: the SNMP server, which runs on the host being monitored, and the SNMP client, which is used for querying the server.
The SNMP server collects information from the host and organizes it in a hierarchical structure, somewhat similar to a tree or the Windows registry, but much simpler. This structure is known as the MIB (Management Information Base), where each node represents a specific piece of data. The root of the tree is empty, and each child node is assigned a unique number. These numbers are connected by dots to form a path that identifies a specific object. For example, the path `.1.3.6.1.2.1.2` refers to the interface information within the MIB-2 group.
The SNMP client sends requests to the server to retrieve information. There are two primary types of requests: GET, which retrieves a specific value, and GETNEXT, which fetches the next available value in the hierarchy. In addition, when an abnormal event occurs on the monitored host, the SNMP server can send a TRAP message to the monitoring system, alerting it to the issue. In this case, the SNMP client acts as a server to process the received trap.
**Second, SNMP Installation**
Most Linux distributions come with SNMP pre-installed. You can check if it's already installed using the command `whereis snmpd`. If not, you can download and install the net-snmp package.
Installation methods include:
1. **Source Installation**:
Run `./configure`, followed by `make` and `make install`.
2. **Package Manager Installation** (for CentOS):
- `yum install net-snmp`
- `yum install net-snmp-devel` (required for development)
- `yum install net-snmp-utils` (needed for testing)
**Third, SNMP Configuration**
After installation, configure the SNMP server. A sample configuration file named `snmpd.conf` is usually included in the source code. Copy this file to a suitable directory, such as `/usr/local/etc/snmp/`, and rename it accordingly.
Inside the configuration file, make the following changes:
1. Modify the `view` directive to allow access to all nodes under the root. For example:
```
view systemview included .1
```
2. Change the `agentAddress` line from `udp:127.0.0.1:161` to the external IP address of the machine so that it can be accessed remotely.
**Fourth, Testing and Troubleshooting**
To test the SNMP service:
1. Start the service using `snmpd` (for package installations) or run the binary directly for source installations.
2. Use `snmpwalk -v 1 localhost -c public .1` on the monitored machine to verify local access.
3. Test remote access using `snmpwalk -v 1 [IP] -c public .1` from the monitoring host.
If remote access fails, disable the firewall (`service iptables stop`) and set SELinux to permissive mode (`setenforce 0`).
**Fifth, SNMP C Programming**
The NET-SNMP library provides a powerful API for integrating custom scripts or programs into the SNMP framework. By adding a line in `snmpd.conf`, you can execute an external program whenever a specific OID is queried.
For example:
```
extend .1.3.6.1.4.1.2021.50 monitor /bin/sh /tmp/monitor.sh
```
This will run the `monitor.sh` script each time the corresponding OID is accessed. The script could be as simple as:
```bash
#!/bin/bash
echo "my snmp test"
```
On the monitoring side, you can write a C program to query and process the results. Here's a basic example:
```c
#include "net-snmp/net-snmp-config.h"
#include "net-snmp/net-snmp-includes.h"
#include
int find_last_oid(netsnmp_session *ss, oid *base, int base_length) {
netsnmp_pdu *response;
netsnmp_pdu *pdu;
int running = 1;
int status;
int length = 0;
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, base, base_length);
while (running) {
status = snmp_synch_response(ss, pdu, &response);
if (status != STAT_SUCCESS || !response) {
snmp_sess_perror("snmp_synch_response", ss);
exit(1);
}
if (response->errstat != SNMP_ERR_NOERROR) {
fprintf(stderr, "snmp: Error in packet: %s\n", snmp_errstring(response->errstat));
exit(1);
}
if (snmp_oid_compare(response->variables->name, SNMP_MIN(base_length, response->variables->name_length), base, base_length) != 0)
running = 0;
else {
memcpy(base, response->variables->name, response->variables->name_length * sizeof(oid));
length = response->variables->name_length;
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, response->variables->name, response->variables->name_length);
}
snmp_free_pdu(response);
}
return length;
}
int main() {
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
struct variable_list *vars;
oid base[128] = {1, 3, 6, 1, 4, 1, 2021, 50};
size_t base_length = 8;
int status;
init_snmp("APC Check");
snmp_sess_init(&session);
session.version = SNMP_VERSION_1;
session.community = (u_char*) "public";
session.community_len = strlen((const char*)session.community);
session.peername = "10.0.1.3"; // IP of the monitored host
ss = snmp_open(&session);
if (!ss) {
snmp_sess_perror("snmp_open", &session);
exit(1);
}
int new_length = find_last_oid(ss, base, base_length);
pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, base, new_length);
status = snmp_synch_response(ss, pdu, &response);
if (status != STAT_SUCCESS || !response) {
snmp_sess_perror("snmp_synch_response", ss);
exit(1);
}
for (vars = response->variables; vars; vars = vars->next_variable) {
for (int i = 0; i < vars->name_length; i++) {
printf("%d.", vars->name[i]);
}
print_value(vars->name, vars->name_length, vars);
}
snmp_free_pdu(response);
snmp_close(ss);
return 0;
}
```
This program queries a specific OID and prints the result. With this foundation, developers can build more complex applications for network monitoring and management.EIB Cable
Smart Switch Kabel,Intelligente Steuersignalleitung,Home Control Signalleitungen,Smart Bus Kabel
Jiangsu D-Bees Smart Home Co., Ltd. , https://www.cI-hometheater.com