Many of these are not really computing issues. Just complex contraptions.
* Goblins entering the fortress are trapped, killed, and disposed of
Should be possible.
* Flooded sections of the fortress are sealed off and drained
Is there any way to sensor water/flooding? Either direct or indirect?
Yes. Pressure plates can be set to only react to specific levels of water/magma. As in, it turns off when the water level goes back down. This one's easy to set up. Just have a seal-and-drain system connected to a pressure plate.
* The mayor can kill any dwarf in the fortress at his leisure
How would you imagine this?
A simpler to use, but more difficult to set up Secret Murders Program:
Have a kill-box for each dwarf. It is a one tile room with a hatch above it that drops in magma from your magma cistern (every good fortress has one). In the room will be a lever that opens the hatch. The (diagonal) entrance has a door that is connected to a Mayoral Lever. Once the room is sealed, The lever inside each room is set to one specific dwarf, and a "pull" command is issued. The pull command cannot be fulfilled unless the mayor pulls his lever.
NOTE: The entrance is diagonal. the magma must be unpressurized.
* A lever in the heart of the computer triggers its selfdestruct
Should be doable.
Self destructs are easy, but the trouble here is that the systems you are describing here don't seem to be subject to any centralization in ONE system. The only way this could be achieved is to arbitrarily make all controls in one room. (my fav' self destruct would be having all controls in the control room made of non-magma safe materials, and have a pressurized magma cistern flood the room and melt everything if you pull "the lever". The lever
must could be surrounded by a 1-frame deployment barrier that protects the "puller" from said magma flood, with an exit opening after all magma drains out.)
* Anyone approaching this lever is killed "I can't let you do that, Urist"
Build traps.
Build traps. Connected to a repeater. Spike traps are good. So are atomsmashing bridges.
* As the computer murders more and more dwarves it will eventually go insane and kill everyone
Hmm, here you would need a counter. Like that thought. Can you link traps to other systems?
If you are certain a dwarf will enter, and not leave, then just have the one-tile entrance to that room have a pressure plate which adds one to the counter (I'll leave the rest to you, I've never successfully built an adder myself.). For example, cluster all the "kill boxes" in a distinct branch of your fortress with a one tile access point. Have an "in" pressure plate notice when a dwarf goes in and add one to the adder. After all, there's no other reason to go in. And no one is leaving.
Same for access to "The Lever".
I'm sure you can figure out a successful "kill everyone" method triggered by the threshold being reached.