If you have a compatible UPS, using apcupsd is probably the easiest way to do this. Simply do 'apt install apcupsd' and it should work almost automatically.
One item from experience: I had to comment out the 'DEVICE' item in apcupsd.conf, otherwise it wouldn't autodetect my USB attached UPS.