**First, the detailed explanation of SNMP programming**
SNMP (Simple Network Management Protocol) is a remote monitoring application that operates over TCP/UDP. It allows network administrators to monitor various aspects of hosts on a network, such as memory usage, CPU utilization, and disk space. The protocol is primarily divided into two components: the SNMP server, which runs on the host being monitored, and the SNMP client, which resides on the monitoring end. The SNMP server collects data from the host and organizes it in a hierarchical structure known as a Management Information Base (MIB). The client sends queries in real-time to retrieve the information.
The SNMP server structures its data in a tree-like format, similar to a file system or registry, but much simpler. This structure is organized using Object Identifiers (OIDs), where each node is assigned a unique number, and the root is empty. These OIDs are used to locate specific pieces of information. For example, the OID `.1.3.6.1.2.1.2` refers to interface-related information under the MIB-2 group.
The SNMP client can send different types of requests. A `GET` request retrieves a specific value, while a `GETNEXT` request fetches the next available OID in sequence. Additionally, the SNMP server can actively send a `TRAP` message to the client when an abnormal event occurs, allowing for real-time alerts and notifications.
**Second, SNMP installation**
Most Linux distributions come with SNMP pre-installed. You can check by running `whereis snmpd`. If it's not installed, you can download and install the net-snmp package.
For source installation:
```bash
./configure; make; make install
```
For package managers like YUM on CentOS:
```bash
yum install net-snmp
```
If you're developing, also install the development tools:
```bash
yum install net-snmp-devel
```
And for testing purposes:
```bash
yum install net-snmp-utils
```
**Third, SNMP configuration**
After installation, the SNMP server needs to be configured. The default configuration file is usually named `snmpd.conf`. You can copy the example configuration file (`snmpd.conf.example`) to your desired location, such as `/usr/local/etc/snmp/`, and rename it to `snmpd.conf`.
In the configuration file, you may need to modify the following settings:
- Allow access to all OIDs by modifying the view definition. For example:
```conf
view systemview included .1
```
- Change the agent address 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**
Once the configuration is complete, start the SNMP service:
- For package installations: `service snmpd start`
- For source installations: `[snmpd absolute path] -c /usr/local/etc/snmp/snmpd.conf`
Test the setup using `snmpwalk`:
```bash
snmpwalk -v 1 localhost -c public .1
```
On the monitoring machine, run:
```bash
snmpwalk -v 1 [IP address of the monitored machine] -c public .1
```
If remote access fails, disable the firewall temporarily:
```bash
service iptables stop
```
Also, adjust SELinux settings if needed:
```bash
setenforce 0
```
**Fifth, SNMP C programming**
The net-snmp library provides a flexible API for developers. You can extend the functionality of the SNMP server by adding custom scripts or programs to the `snmpd.conf` file. When a query is made, the program is executed automatically.
For example, you can add this line to `snmpd.conf`:
```conf
extend .1.3.6.1.4.1.2021.50 monitor /bin/sh /tmp/monitor.sh
```
This will execute the script `monitor.sh` whenever the OID `.1.3.6.1.4.1.2021.50` is queried. The script could look like this:
```bash
#!/bin/bash
echo "my snmp test"
```
On the client side, you can write a C program to interact with the SNMP server. 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 (response->variables && 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 address 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) {
int i;
for (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 demonstrates how to query an SNMP agent and process the returned values. It uses the net-snmp library to handle SNMP messages and extract the required data.Fabric Painted Starry Sky Ceiling
Widely applicable to a wide range of scenarios, customizable according to individual needs, and able to freely guide light according to different creative ideas, showcasing personalized charm.
Fabric Painted Starry Sky Ceiling,Fabric Painted Starry Sky Ceiling For Home, Diy Fabric Painted Starry Sky Ceiling,Fabric Starry Sky Ceiling
Jiangsu D-Bees Smart Home Co., Ltd. , https://www.cI-hometheater.com