Sussed this out eventually in case anyone else wants to automate adding devices:or create users etc. manually, this way when a new device is deployed it is automatically added to the users permissions list
SSH to NMS
Run the add device with device IP and community string, update SQL device permissions
$z4 I pull from the VM I deploy seperately
set_include_path(get_include_path() . PATH_SEPARATOR . ‘/var/includes/ssh’);
include ‘/var/includes/ssh/Net/SSH2.php’;
include ‘/var/includes/pdo-nms.php’;
// create a new NMS user
// NMS IP address
$nms=“1.1.1.1”;
// SSH to NMS and run adduser script
$ssh = new Net_SSH2($nms);
if (!$ssh->login(‘root’, ‘PASSWORDOFSOMESORT’)) {
exit(‘Login Failed’);
} else {
$resp= $ssh->exec(“php /opt/librenms/addhost.php -g 0 -f -p ifName " . $z4 . " ‘SNMPCOUMUNITY’ v2c 161 udp”);
// Amend user permissions in LibreNMS SQl to allow them access to this device
// get the device ID from SQL
$stmtnms1 = $pdonms->prepare(“SELECT device_id FROM devices WHERE hostname = ?”);
$stmtnms1->execute([$z4]);
$rownms1=$stmtnms1->fetch();
$d=$rownms1[‘device_id’];
$stmtnms1=NULL;
// Update permissions for the user
$stmtnms1 = $pdonms->prepare(“INSERT INTO devices_perms (user_id, device_id) VALUES (?,?)”);
$stmtnms1->execute([$nmsId,$d]);
$stmtnms1=NULL;
For setting up a user
set_include_path(get_include_path() . PATH_SEPARATOR . ‘/var/includes/ssh’);
include ‘/var/includes/ssh/Net/SSH2.php’;
include ‘/var/includes/pdo-nms.php’;
// create a new NMS user
// NMS IP address
$nms=“1.1.1.1”;
// SSH to NMS and run adduser script
$ssh = new Net_SSH2($nms);
if (!$ssh->login(‘root’, ‘PASSWORD’)) {
exit(‘Login Failed’);
} else {
$resp= $ssh->exec(‘php /opt/librenms/adduser.php ’ . $email . ’ ’ . $pass . ’ 1’ );
// update SQL
$stmtnms1 = $pdonms->prepare(“SELECT user_id FROM users WHERE username = ?”);
$stmtnms1->execute([$email]);
$rownms1=$stmtnms1->fetch();
$i=$rownms1[‘user_id’];
$stmtnms1=NULL;
// Now update
$stmtnms2 = $pdo->prepare(“UPDATE users SET nmsId = ? WHERE userEmail = ?”);
$stmtnms2->execute([(int)$i,$email]);
$stmtnms2=NULL;
}
This then updates my sites SQL with the NMS ID which I pull and use when adding the device.
For each device I run the following startup script on firstboot
Script
apt-get update
echo ‘retrieving Distro agent LibreNMS\n’
sudo curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
sudo wget -P /etc/one-context.d/ http://1.1.1.1/scripts/snmpcentos.sh
sudo chmod +x /etc/one-context.d/snmpcentos.sh
sudo bash /etc/one-context.d/snmpcentos.sh
sudo service snmpd restart
rm -f /etc/one-context.d/snmpcentos.sh
chmod 444 /sys/devices/virtual/dmi/id/product_serial
chmod +x /usr/bin/distro
Script from NMS server
- create a directory so servers can pull these scripts
echo “Installing SNMPD\n”
sudo apt-get install snmpd -y
## Script for OS on boot, install snmpd and set community
echo “Creating SNMP config for Piggybank NMS\n”
cat > /etc/snmp/snmpd.conf << EOF
# Allow SNMP only from Piggybank Cloud NMS
rocommunity COMMUNITY 1.1.1.1/32
group MyROGroup v2c readonly
view all included .1 80
access MyROGroup “” any noauth exact all none none
sysLocation Piggybank Cloud - Leeds UK
sysContact Piggybank Cloud [email protected]
#Distro Detection
extend .1.3.6.1.4.1.2021.7890.1 distro /usr/bin/distro
#SNMP Extends
extend .1.3.6.1.4.1.2021.7890.2 hardware ‘/bin/cat /sys/devices/virtual/dmi/id/product_name’
extend .1.3.6.1.4.1.2021.7890.3 manufacturer ‘/bin/cat /sys/devices/virtual/dmi/id/sys_vendor’
extend .1.3.6.1.4.1.2021.7890.4 serial ‘/bin/cat /sys/devices/virtual/dmi/id/product_serial’
EOF
echo “Restarting snmpd\n”
service snmpd restart