install-eye.en.sh 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. #!/bin/bash
  2. # Eye Installation Script for ALT Linux/Debian/Ubuntu
  3. # Version: 2.0
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Output functions
  12. print_info() {
  13. echo -e "${GREEN}[INFO]${NC} $1"
  14. }
  15. print_warn() {
  16. echo -e "${YELLOW}[WARN]${NC} $1"
  17. }
  18. print_error() {
  19. echo -e "${RED}[ERROR]${NC} $1"
  20. }
  21. print_step() {
  22. echo -e "${BLUE}=== $1 ===${NC}"
  23. }
  24. # Check for root privileges
  25. check_root() {
  26. if [[ $EUID -ne 0 ]]; then
  27. print_error "This script must be run as root"
  28. print_error "Use: sudo $0"
  29. exit 1
  30. fi
  31. }
  32. # Detect distribution and package manager
  33. detect_distro() {
  34. if [[ -f /etc/os-release ]]; then
  35. . /etc/os-release
  36. OS_ID=$ID
  37. OS_VERSION=$VERSION_ID
  38. OS_NAME=$NAME
  39. case $OS_ID in
  40. altlinux)
  41. PACKAGE_MANAGER="apt-get"
  42. SERVICE_MANAGER="systemctl"
  43. OS_FAMILY="alt"
  44. print_info "Detected ALT Linux $OS_VERSION"
  45. ;;
  46. debian)
  47. PACKAGE_MANAGER="apt"
  48. SERVICE_MANAGER="systemctl"
  49. OS_FAMILY="debian"
  50. print_info "Detected Debian $OS_VERSION"
  51. ;;
  52. ubuntu)
  53. PACKAGE_MANAGER="apt"
  54. SERVICE_MANAGER="systemctl"
  55. OS_FAMILY="debian"
  56. print_info "Detected Ubuntu $OS_VERSION"
  57. ;;
  58. *)
  59. print_error "Unsupported distribution: $OS_ID"
  60. print_error "Supported: ALT Linux, Debian, Ubuntu"
  61. exit 1
  62. ;;
  63. esac
  64. else
  65. print_error "Failed to detect distribution"
  66. exit 1
  67. fi
  68. }
  69. # Install dependencies for ALT Linux
  70. install_deps_altlinux() {
  71. print_step "Installing dependencies for ALT Linux"
  72. # Update repositories
  73. apt-get update
  74. # General utilities
  75. apt-get install -y git xxd wget fping hwdata
  76. # Database
  77. apt-get install -y mariadb-server mariadb-client
  78. # Web server and PHP
  79. apt-get install -y apache2 \
  80. php8.2 php8.2-mysqlnd php8.2-intl php8.2-mbstring \
  81. pear-Mail php8.2-snmp php8.2-zip \
  82. php8.2-pgsql php8.2-mysqlnd php8.2-pdo_mysql php8.2-mysqlnd-mysqli \
  83. php8.2-fpm-fcgi apache2-mod_fcgid
  84. # Perl modules
  85. apt-get install -y perl perl-Net-Patricia perl-NetAddr-IP \
  86. perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Net-Ping \
  87. perl-Net-Netmask perl-Text-Iconv perl-Net-SNMP \
  88. perl-Net-Telnet perl-DBI perl-DBD-mysql perl-DBD-Pg \
  89. perl-Parallel-ForkManager perl-Proc-Daemon \
  90. perl-DateTime-Format-DateParse \
  91. perl-Net-OpenSSH perl-File-Tail perl-Crypt-Rijndael \
  92. perl-Crypt-CBC perl-CryptX perl-Crypt-DES \
  93. perl-File-Path-Tiny perl-Expect \
  94. perl-Proc-ProcessTable
  95. # Additional services
  96. apt-get install -y dnsmasq syslog-ng syslog-ng-journal
  97. # Install pwgen if not present
  98. if ! command -v pwgen &> /dev/null; then
  99. apt-get install -y pwgen
  100. fi
  101. control fping public
  102. control ping public
  103. }
  104. # Install dependencies for Debian/Ubuntu
  105. install_deps_debian() {
  106. print_step "Installing dependencies for Debian/Ubuntu"
  107. # Update repositories
  108. apt-get update
  109. # General utilities
  110. apt-get install -y git xxd bsdmainutils pwgen wget fping ieee-data
  111. # Database
  112. apt-get install -y mariadb-server mariadb-client
  113. # Web server and PHP
  114. apt-get install -y apache2 \
  115. php php-mysql php-bcmath php-intl php-mbstring \
  116. php-date php-mail php-snmp php-zip \
  117. php-db php-pgsql php-fpm libapache2-mod-fcgid
  118. # Perl modules
  119. apt-get install -y perl libnet-patricia-perl libnetaddr-ip-perl \
  120. libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
  121. libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl \
  122. libnet-telnet-perl libdbi-perl libdbd-mysql-perl \
  123. libparallel-forkmanager-perl libproc-daemon-perl \
  124. libdatetime-format-dateparse-perl \
  125. libnet-openssh-perl libfile-tail-perl libcrypt-rijndael-perl \
  126. libcrypt-cbc-perl libcryptx-perl libdbd-pg-perl \
  127. libfile-path-tiny-perl libexpect-perl libcrypt-des-perl
  128. # Additional services
  129. apt-get install -y dnsmasq syslog-ng
  130. }
  131. # System update
  132. update_system() {
  133. print_step "Updating system"
  134. $PACKAGE_MANAGER update -y
  135. }
  136. # Install packages
  137. install_packages() {
  138. print_step "Installing packages"
  139. case $OS_FAMILY in
  140. alt)
  141. install_deps_altlinux
  142. ;;
  143. debian)
  144. install_deps_debian
  145. ;;
  146. esac
  147. }
  148. # Create user and group
  149. create_user_group() {
  150. print_step "Creating user and group"
  151. # Create group
  152. if ! getent group eye >/dev/null; then
  153. groupadd --system eye
  154. print_info "Group 'eye' created"
  155. else
  156. print_info "Group 'eye' already exists"
  157. fi
  158. # Create user
  159. if ! id -u eye >/dev/null 2>&1; then
  160. if [[ "$OS_FAMILY" == "alt" ]]; then
  161. # For ALT Linux
  162. useradd --system --shell /bin/bash --home-dir /opt/Eye \
  163. --gid eye --groups eye eye
  164. else
  165. # For Debian/Ubuntu
  166. adduser --system --disabled-password --disabled-login \
  167. --ingroup eye --home=/opt/Eye eye
  168. fi
  169. print_info "User 'eye' created"
  170. else
  171. print_info "User 'eye' already exists"
  172. fi
  173. # Create directory
  174. mkdir -p /opt/Eye
  175. chown eye:eye /opt/Eye
  176. chmod 770 /opt/Eye
  177. # Add nagios to eye group (if exists)
  178. if id -u nagios >/dev/null 2>&1; then
  179. usermod -a -G eye nagios
  180. print_info "User 'nagios' added to group 'eye'"
  181. fi
  182. }
  183. # Check and apply SNMP SHA512 patch
  184. apply_snmp_patch() {
  185. print_info "Checking for SNMPv3 SHA512 support..."
  186. # File paths
  187. USM_PATCH_FILE="/opt/Eye/docs/patches/sha512.patch"
  188. if [[ "$OS_FAMILY" == "alt" ]]; then
  189. USM_PATCH_FILE="/opt/Eye/docs/patches/sha512.alt.patch"
  190. fi
  191. USM_PM_FILE=""
  192. # Search for USM.pm in system
  193. local usm_paths=(
  194. "/usr/share/perl5/Net/SNMP/Security/USM.pm"
  195. "/usr/lib/perl5/vendor_perl/Net/SNMP/Security/USM.pm"
  196. "/usr/local/share/perl5/Net/SNMP/Security/USM.pm"
  197. )
  198. for path in "${usm_paths[@]}"; do
  199. if [[ -f "$path" ]]; then
  200. USM_PM_FILE="$path"
  201. print_info "Found USM.pm: $USM_PM_FILE"
  202. break
  203. fi
  204. done
  205. if [[ -z "$USM_PM_FILE" ]]; then
  206. print_warn "USM.pm file not found in system"
  207. return 1
  208. fi
  209. # Check if patch already applied
  210. if grep -q "AUTH_PROTOCOL_HMACSHA512" "$USM_PM_FILE"; then
  211. print_info "SHA512 patch already applied"
  212. return 0
  213. fi
  214. # Create backup
  215. cp "$USM_PM_FILE" "${USM_PM_FILE}.backup"
  216. print_info "Backup created: ${USM_PM_FILE}.backup"
  217. # Try to apply patch file
  218. local patch_applied=false
  219. if [[ -f "$USM_PATCH_FILE" ]]; then
  220. print_info "Attempting to apply patch from $USM_PATCH_FILE"
  221. # Check if patch can be applied
  222. if patch --dry-run -l -p1 -i "$USM_PATCH_FILE" -r /tmp/patch.rej "$USM_PM_FILE" 2>/dev/null; then
  223. # Apply patch
  224. if patch -l -p1 -i "$USM_PATCH_FILE" "$USM_PM_FILE" 2>/dev/null; then
  225. print_info "Patch successfully applied!"
  226. patch_applied=true
  227. else
  228. print_warn "Failed to apply patch (dry-run passed but actual application failed)"
  229. fi
  230. else
  231. print_warn "Patch cannot be applied automatically (version mismatch)"
  232. # Check differences
  233. print_info "Checking patch differences..."
  234. if [[ -f "/opt/Eye/docs/patches/USM.pm" ]]; then
  235. diff -u "$USM_PM_FILE" "/opt/Eye/docs/patches/USM.pm" > /tmp/usm.diff 2>/dev/null || true
  236. if [[ -s /tmp/usm.diff ]]; then
  237. print_warn "Differences found in USM.pm file"
  238. echo "Differences:"
  239. head -20 /tmp/usm.diff
  240. echo "..."
  241. fi
  242. fi
  243. fi
  244. fi
  245. # If patch not applied, ask user
  246. if [[ "$patch_applied" == false ]]; then
  247. echo ""
  248. print_warn "Automatic patch application failed"
  249. print_warn "Modification of USM.pm file required for SNMPv3 with SHA512 support"
  250. echo ""
  251. read -p "Do you need SNMPv3 SHA512 support? (y/n): " -n 1 -r
  252. echo
  253. if [[ $REPLY =~ ^[Yy]$ ]]; then
  254. # Try to replace the entire file
  255. if [[ -f "/opt/Eye/docs/patches/USM.pm" ]]; then
  256. print_info "Replacing USM.pm file entirely..."
  257. # Check version compatibility
  258. local original_ver=$(grep -i "version" "$USM_PM_FILE" | head -1)
  259. local patch_ver=$(grep -i "version" "/opt/Eye/docs/patches/USM.pm" | head -1)
  260. if [[ -n "$original_ver" && -n "$patch_ver" ]]; then
  261. print_info "Original file version: $original_ver"
  262. print_info "Patch version: $patch_ver"
  263. fi
  264. # Create additional backup
  265. cp "$USM_PM_FILE" "${USM_PM_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
  266. # Replace file
  267. cp -f "/opt/Eye/docs/patches/USM.pm" "$USM_PM_FILE"
  268. # Check if replacement successful
  269. if grep -q "SHA-512" "$USM_PM_FILE"; then
  270. print_info "USM.pm file successfully replaced, SHA512 support added"
  271. # Save replacement info
  272. echo "USM.pm file was replaced for SHA512 support" > "${USM_PM_FILE}.replaced"
  273. echo "Original file saved as: ${USM_PM_FILE}.backup" >> "${USM_PM_FILE}.replaced"
  274. echo "Replacement date: $(date)" >> "${USM_PM_FILE}.replaced"
  275. return 0
  276. else
  277. print_error "Failed to add SHA512 support after file replacement"
  278. # Restore from backup
  279. cp "${USM_PM_FILE}.backup" "$USM_PM_FILE"
  280. return 1
  281. fi
  282. else
  283. print_error "Patched USM.pm file not found in /opt/Eye/docs/patches/"
  284. return 1
  285. fi
  286. else
  287. print_info "SNMPv3 SHA512 support disabled"
  288. return 0
  289. fi
  290. fi
  291. return 0
  292. }
  293. # Download and copy source code
  294. install_source_code() {
  295. print_step "Installing Eye source code"
  296. # Create directory structure
  297. print_info "Creating directory structure..."
  298. mkdir -p /opt/Eye/scripts/cfg
  299. mkdir -p /opt/Eye/scripts/log
  300. mkdir -p /opt/Eye/html/cfg
  301. mkdir -p /opt/Eye/html/js
  302. mkdir -p /opt/Eye/docs
  303. chmod -R 755 /opt/Eye/html
  304. chmod -R 770 /opt/Eye/scripts/log
  305. chmod 750 /opt/Eye/scripts
  306. # Copy files
  307. print_info "Copying files..."
  308. cp -R scripts/ /opt/Eye/
  309. cp -R html/ /opt/Eye/
  310. cp -R docs/ /opt/Eye/
  311. # Set permissions
  312. chown -R eye:eye /opt/Eye
  313. # Apply SNMP SHA512 patch
  314. apply_snmp_patch
  315. }
  316. # Download additional scripts
  317. download_additional_scripts() {
  318. print_step "Downloading additional scripts"
  319. # Create directories
  320. mkdir -p /opt/Eye/html/js/jq
  321. mkdir -p /opt/Eye/html/js/select2
  322. mkdir -p /opt/Eye/html/js/jstree
  323. # Download jQuery
  324. print_info "Downloading jQuery..."
  325. if ! wget -q https://code.jquery.com/jquery-3.7.0.min.js \
  326. -O /opt/Eye/html/js/jq/jquery.min.js; then
  327. print_warn "Failed to download jQuery, trying alternative source..."
  328. wget -q https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js \
  329. -O /opt/Eye/html/js/jq/jquery.min.js || \
  330. print_error "Failed to download jQuery"
  331. fi
  332. # Download Select2
  333. print_info "Downloading Select2..."
  334. if wget -q https://github.com/select2/select2/archive/4.0.12.tar.gz -O 4.0.12.tar.gz; then
  335. tar -xzf 4.0.12.tar.gz -C /opt/Eye/html/js/select2/ \
  336. --strip-components=2 select2-4.0.12/dist 2>/dev/null || \
  337. tar -xzf 4.0.12.tar.gz -C /opt/Eye/html/js/select2/ \
  338. --strip-components=1 select2-4.0.12/dist 2>/dev/null
  339. rm -f 4.0.12.tar.gz
  340. else
  341. print_warn "Failed to download Select2"
  342. fi
  343. # Download jsTree
  344. print_info "Downloading jsTree..."
  345. if wget -q https://github.com/vakata/jstree/archive/3.3.12.tar.gz -O jstree.tar.gz; then
  346. tar -xzf jstree.tar.gz -C /opt/Eye/html/js/
  347. mv /opt/Eye/html/js/jstree-3.3.12/dist /opt/Eye/html/js/jstree
  348. rm -rf /opt/Eye/html/js/jstree-3.3.12
  349. rm -f jstree.tar.gz
  350. else
  351. print_warn "Failed to download jsTree"
  352. fi
  353. # Set permissions
  354. chown -R eye:eye /opt/Eye/html/js
  355. }
  356. # Configure MySQL
  357. setup_mysql() {
  358. print_step "Configuring MySQL"
  359. # Start and enable service
  360. $SERVICE_MANAGER enable mariadb 2>/dev/null || \
  361. $SERVICE_MANAGER enable mysql 2>/dev/null || true
  362. $SERVICE_MANAGER start mariadb 2>/dev/null || \
  363. $SERVICE_MANAGER start mysql 2>/dev/null || true
  364. # Check MySQL access
  365. if ! command -v mysql &> /dev/null; then
  366. print_error "MySQL client not installed"
  367. return 1
  368. fi
  369. MYSQL_OPT="-u root"
  370. # Check access without password
  371. if mysql -u root -e "SELECT 1;" 2>/dev/null; then
  372. print_info "MySQL accessible with empty password"
  373. echo ""
  374. print_warn "IMPORTANT: Need to set root password for MySQL!"
  375. print_warn "After installation run: mysql_secure_installation"
  376. echo ""
  377. else
  378. # Ask for password and create config file
  379. read -p "Enter MySQL root user password: " DB_ROOT_PASSWORD
  380. echo ""
  381. # Create temporary config file
  382. MYSQL_CNF_FILE="/tmp/mysql_root_eye.cnf"
  383. echo "[client]" > "$MYSQL_CNF_FILE"
  384. echo "user=root" >> "$MYSQL_CNF_FILE"
  385. echo "password=$DB_ROOT_PASSWORD" >> "$MYSQL_CNF_FILE"
  386. chmod 600 "$MYSQL_CNF_FILE"
  387. # Check connection
  388. if mysql --defaults-extra-file="$MYSQL_CNF_FILE" -e "SELECT 1;" &>/dev/null; then
  389. print_info "Successfully connected to MySQL"
  390. MYSQL_OPT="--defaults-extra-file=$MYSQL_CNF_FILE"
  391. else
  392. print_error "Incorrect MySQL root password"
  393. rm -f "$MYSQL_CNF_FILE"
  394. return 1
  395. fi
  396. fi
  397. read -p "Create database and user for Eye? (y/n): " -n 1 -r
  398. echo
  399. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  400. print_warn "Database creation skipped. Create manually:"
  401. print_warn " mysql -u root -p < /opt/Eye/docs/mysql/create_db.sql"
  402. print_warn " mysql -u root -p stat < /opt/Eye/docs/mysql/latest-mysql-en.sql"
  403. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  404. rm -f "$MYSQL_CNF_FILE"
  405. fi
  406. return 0
  407. fi
  408. # Generate password for stat user
  409. DB_PASSWORD=$(pwgen 16 1)
  410. MYSQL_PASSWORD=$DB_PASSWORD
  411. print_info "Importing database structure..."
  412. # Import main SQL file
  413. mysql $MYSQL_OPT < /opt/Eye/docs/mysql/create_db.sql
  414. if [[ $? -ne 0 ]]; then
  415. print_error "Error importing create_db.sql"
  416. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  417. rm -f "$MYSQL_CNF_FILE"
  418. fi
  419. return 1
  420. fi
  421. print_info "Database structure imported"
  422. # Import data
  423. print_info "Importing initial data..."
  424. mysql $MYSQL_OPT stat < /opt/Eye/docs/mysql/latest-mysql-en.sql
  425. if [[ $? -ne 0 ]]; then
  426. print_warn "Error importing latest-mysql-en.sql (data may already exist)"
  427. else
  428. print_info "Initial data imported"
  429. fi
  430. # Create stat user
  431. print_info "Creating user 'stat'..."
  432. mysql $MYSQL_OPT <<EOF
  433. CREATE USER IF NOT EXISTS 'stat'@'localhost' IDENTIFIED BY '$DB_PASSWORD';
  434. GRANT ALL PRIVILEGES ON stat.* TO 'stat'@'localhost';
  435. FLUSH PRIVILEGES;
  436. EOF
  437. if [[ $? -ne 0 ]]; then
  438. print_error "Error creating user 'stat'"
  439. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  440. rm -f "$MYSQL_CNF_FILE"
  441. fi
  442. return 1
  443. fi
  444. print_info "User 'stat' successfully created"
  445. # Save password information
  446. echo "MySQL 'stat' user password: $DB_PASSWORD" > /root/eye_mysql_password.txt
  447. chmod 600 /root/eye_mysql_password.txt
  448. print_info "User 'stat' password: $DB_PASSWORD"
  449. print_warn "Password saved in /root/eye_mysql_password.txt"
  450. # Clean up temporary file if created
  451. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  452. rm -f "$MYSQL_CNF_FILE"
  453. fi
  454. return 0
  455. }
  456. # Configure configuration files
  457. setup_configs() {
  458. print_step "Configuring configuration files"
  459. # Copy configuration files
  460. if [[ -f "/opt/Eye/html/cfg/config.sample.php" ]]; then
  461. cp /opt/Eye/html/cfg/config.sample.php /opt/Eye/html/cfg/config.php
  462. fi
  463. if [[ -f "/opt/Eye/scripts/cfg/config.sample" ]]; then
  464. cp /opt/Eye/scripts/cfg/config.sample /opt/Eye/scripts/cfg/config
  465. fi
  466. # Generate encryption keys
  467. print_info "Generating encryption keys..."
  468. if command -v pwgen &> /dev/null; then
  469. ENC_PASSWORD=$(pwgen 16 1)
  470. else
  471. ENC_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c16)
  472. fi
  473. ENC_VECTOR=$(tr -dc 0-9 </dev/urandom | head -c 16)
  474. # Configure config.php
  475. if [[ -f "/opt/Eye/html/cfg/config.sample.php" ]]; then
  476. cp /opt/Eye/html/cfg/config.sample.php /opt/Eye/html/cfg/config.php
  477. # Update database password
  478. if [[ -n "$MYSQL_PASSWORD" ]]; then
  479. sed -i "s/define(\"DB_PASS\",\"[^\"]*\");/define(\"DB_PASS\",\"$MYSQL_PASSWORD\");/" /opt/Eye/html/cfg/config.php
  480. sed -i "s/define(\"DB_NAME\",\"[^\"]*\");/define(\"DB_NAME\",\"stat\");/" /opt/Eye/html/cfg/config.php
  481. sed -i "s/define(\"DB_USER\",\"[^\"]*\");/define(\"DB_USER\",\"stat\");/" /opt/Eye/html/cfg/config.php
  482. fi
  483. # Replacing the Russian locale with the English
  484. sed -i "s/setlocale(LC_ALL, 'ru_RU\.UTF8');/setlocale(LC_ALL, 'en_US.UTF-8');/g" /opt/Eye/html/cfg/config.php
  485. # Replacing HTML_LANG with English
  486. sed -i "s/define(\"HTML_LANG\",\"russian\");/define(\"HTML_LANG\",\"english\");/g" /opt/Eye/html/cfg/config.php
  487. # Update encryption key
  488. sed -i "s/ENCRYPTION_KEY\",\"[^\"]*\"/ENCRYPTION_KEY\",\"$ENC_PASSWORD\"/" /opt/Eye/html/cfg/config.php
  489. sed -i "s/ENCRYPTION_KEY','[^']*'/ENCRYPTION_KEY','$ENC_PASSWORD'/" /opt/Eye/html/cfg/config.php
  490. # Update initialization vector
  491. sed -i "s/ENCRYPTION_IV\",\"[^\"]*\"/ENCRYPTION_IV\",\"$ENC_VECTOR\"/" /opt/Eye/html/cfg/config.php
  492. sed -i "s/ENCRYPTION_IV','[^']*'/ENCRYPTION_IV','$ENC_VECTOR'/" /opt/Eye/html/cfg/config.php
  493. print_info "Configuration file config.php created from template"
  494. fi
  495. # Configure config for scripts
  496. if [[ -f "/opt/Eye/scripts/cfg/config.sample" ]]; then
  497. cp /opt/Eye/scripts/cfg/config.sample /opt/Eye/scripts/cfg/config
  498. # Update database password
  499. if [[ -n "$MYSQL_PASSWORD" ]]; then
  500. sed -i "s/^DBPASS=.*/DBPASS=$MYSQL_PASSWORD/" /opt/Eye/scripts/cfg/config
  501. sed -i "s/DBPASS=mysql_password/DBPASS=$MYSQL_PASSWORD/" /opt/Eye/scripts/cfg/config
  502. fi
  503. # Update database username
  504. sed -i "s/^DBUSER=.*/DBUSER=stat/" /opt/Eye/scripts/cfg/config
  505. sed -i "s/DBUSER=mysql_user/DBUSER=stat/" /opt/Eye/scripts/cfg/config
  506. # Update database name
  507. sed -i "s/^DBNAME=.*/DBNAME=stat/" /opt/Eye/scripts/cfg/config
  508. sed -i "s/DBNAME=mysql_database/DBNAME=stat/" /opt/Eye/scripts/cfg/config
  509. # Update encryption key
  510. sed -i "s/^encryption_key=.*/encryption_key=$ENC_PASSWORD/" /opt/Eye/scripts/cfg/config
  511. sed -i "s/encryption_key=!!!CHANGE_ME!!!!/encryption_key=$ENC_PASSWORD/" /opt/Eye/scripts/cfg/config
  512. # Update initialization vector
  513. sed -i "s/^encryption_iv=.*/encryption_iv=$ENC_VECTOR/" /opt/Eye/scripts/cfg/config
  514. sed -i "s/encryption_iv=0123456789012345/encryption_iv=$ENC_VECTOR/" /opt/Eye/scripts/cfg/config
  515. print_info "Configuration file scripts/cfg/config created from template"
  516. fi
  517. # Set permissions
  518. chown -R eye:eye /opt/Eye/html/cfg /opt/Eye/scripts/cfg
  519. chmod 660 /opt/Eye/html/cfg/config.php /opt/Eye/scripts/cfg/config
  520. chmod 750 /opt/Eye/html/cfg /opt/Eye/scripts/cfg
  521. print_info "Encryption keys generated"
  522. print_info "Password: $ENC_PASSWORD"
  523. print_info "Vector: $ENC_VECTOR"
  524. }
  525. # Configure Apache and PHP
  526. setup_apache_php() {
  527. print_step "Configuring Apache and PHP"
  528. # Determine PHP version
  529. PHP_VERSION=$(php -v 2>/dev/null | head -n1 | grep -oP '\d+\.\d+' || echo "8.1")
  530. # Configure PHP for all distributions
  531. if [[ "$OS_FAMILY" == "alt" ]]; then
  532. # ALT Linux
  533. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  534. APACHE_CONF_DIR="/etc/httpd2/conf"
  535. APACHE_SITES_DIR="$APACHE_CONF_DIR/sites-available"
  536. DEFAULT_CONF="$APACHE_SITES_DIR/000-default.conf"
  537. APACHE_USER="apache2"
  538. else
  539. # Debian/Ubuntu
  540. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  541. APACHE_CONF_DIR="/etc/apache2"
  542. APACHE_SITES_DIR="$APACHE_CONF_DIR/sites-available"
  543. DEFAULT_CONF="$APACHE_SITES_DIR/000-default.conf"
  544. APACHE_USER="www-data"
  545. fi
  546. # Configure Apache
  547. if [[ -f "/opt/Eye/docs/apache/000-default.conf" ]]; then
  548. print_info "Using prepared Apache template for ALT Linux"
  549. # Create directory if it doesn't exist
  550. mkdir -p "$APACHE_SITES_DIR"
  551. # Copy prepared config
  552. cp "/opt/Eye/docs/apache/000-default.conf" "$DEFAULT_CONF"
  553. # Enable site
  554. if [[ -f "$APACHE_CONF_DIR/sites-enabled/000-default.conf" ]]; then
  555. rm -f "$APACHE_CONF_DIR/sites-enabled/000-default.conf"
  556. ln -sf "$DEFAULT_CONF" "$APACHE_CONF_DIR/sites-enabled/000-default.conf"
  557. fi
  558. fi
  559. # Configure sudoers
  560. if [[ -f "/opt/Eye/docs/sudoers.d/www-data" ]]; then
  561. # Use prepared template, substituting correct user
  562. sed "s/www-data/eye/g" /opt/Eye/docs/sudoers.d/www-data > /etc/sudoers.d/eye
  563. chmod 440 /etc/sudoers.d/eye
  564. print_info "Sudoers file created from template"
  565. fi
  566. # Restart Apache
  567. if [[ "$OS_FAMILY" == "alt" ]]; then
  568. # ALT Linux uses httpd2
  569. APACHE_SERVICE="httpd2"
  570. else
  571. APACHE_SERVICE="apache2"
  572. fi
  573. # usermod -a -G eye $APACHE_USER
  574. if [[ "$OS_FAMILY" == "debian" ]]; then
  575. a2dismod php${PHP_VERSION} 2>/dev/null
  576. a2dismod mpm_prefork 2>/dev/null
  577. a2enmod mpm_event 2>/dev/null
  578. a2enconf php${PHP_VERSION}-fpm 2>/dev/null
  579. fi
  580. mkdir -p /var/log/php-fpm/
  581. a2enmod setenvif
  582. a2enmod proxy
  583. a2enmod proxy_fcgi
  584. print_info "Apache configured, sudoers user: $APACHE_USER"
  585. print_info "Apache service: $APACHE_SERVICE"
  586. # Configure php-fpm
  587. print_info "Configure php-fpm${PHP_VERSION}"
  588. if [[ -f "/opt/Eye/docs/php-fpm/eye.conf" ]]; then
  589. print_info "Using prepared php-fpm template"
  590. if [[ "$OS_FAMILY" == "alt" ]]; then
  591. cp "/opt/Eye/docs/php-fpm/eye.conf" /etc/fpm${PHP_VERSION}/php-fpm.d/
  592. else
  593. cp "/opt/Eye/docs/php-fpm/eye.conf" /etc/php/${PHP_VERSION}/fpm/pool.available/
  594. ln -sf "/etc/php/${PHP_VERSION}/fpm/pool.available/eye.conf" "/etc/php/${PHP_VERSION}/fpm/pool.d/eye.conf"
  595. fi
  596. fi
  597. $SERVICE_MANAGER enable "$APACHE_SERVICE"
  598. $SERVICE_MANAGER restart "$APACHE_SERVICE"
  599. $SERVICE_MANAGER enable php${PHP_VERSION}-fpm.service
  600. $SERVICE_MANAGER restart php${PHP_VERSION}-fpm.service
  601. # Check configuration
  602. if [[ "$OS_FAMILY" == "alt" ]]; then
  603. httpd2 -t 2>/dev/null && print_info "Apache (httpd2) configuration is valid" || print_warn "Check Apache configuration"
  604. else
  605. apache2ctl -t 2>/dev/null && print_info "Apache configuration is valid" || print_warn "Check Apache configuration"
  606. fi
  607. }
  608. # Configure cron and logrotate
  609. setup_cron_logrotate() {
  610. print_step "Configuring cron and logrotate"
  611. # Cron
  612. if [[ -f "/opt/Eye/docs/cron/stat" ]]; then
  613. cp /opt/Eye/docs/cron/stat /etc/cron.d/eye
  614. chmod 644 /etc/cron.d/eye
  615. print_info "Cron job added: /etc/cron.d/eye"
  616. fi
  617. # Logrotate
  618. if [[ -f "/opt/Eye/docs/logrotate/dnsmasq" ]]; then
  619. cp /opt/Eye/docs/logrotate/dnsmasq /etc/logrotate.d/dnsmasq-eye
  620. fi
  621. if [[ -f "/opt/Eye/docs/logrotate/scripts" ]]; then
  622. cp /opt/Eye/docs/logrotate/scripts /etc/logrotate.d/eye-scripts
  623. fi
  624. print_info "Cron and logrotate configuration completed"
  625. print_warn "Edit /etc/cron.d/eye to enable required scripts"
  626. }
  627. # Configure DHCP server (dnsmasq)
  628. setup_dhcp_server() {
  629. print_step "Configuring DHCP server"
  630. read -p "Configure DHCP server (dnsmasq)? (y/n): " -n 1 -r
  631. echo
  632. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  633. return 0
  634. fi
  635. # Backup configuration
  636. if [[ -f "/etc/dnsmasq.conf" ]]; then
  637. cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
  638. fi
  639. # Copy configuration from Eye
  640. if [[ -f "/opt/Eye/docs/addons/dnsmasq.conf" ]]; then
  641. cat /opt/Eye/docs/addons/dnsmasq.conf > /etc/dnsmasq.conf
  642. fi
  643. # Copy systemd services
  644. if [[ -f "/opt/Eye/docs/systemd/dhcp-log.service" ]]; then
  645. cp /opt/Eye/docs/systemd/dhcp-log.service /etc/systemd/system/
  646. fi
  647. if [[ -f "/opt/Eye/docs/systemd/dhcp-log-truncate.service" ]]; then
  648. cp /opt/Eye/docs/systemd/dhcp-log-truncate.service /etc/systemd/system/
  649. fi
  650. # Enable services
  651. $SERVICE_MANAGER enable dnsmasq
  652. $SERVICE_MANAGER start dnsmasq
  653. print_info "DHCP server configured"
  654. print_warn "Edit /etc/dnsmasq.conf for your network"
  655. }
  656. # Configure syslog-ng
  657. setup_syslog() {
  658. print_step "Configuring syslog-ng"
  659. read -p "Configure remote log collection (syslog-ng)? (y/n): " -n 1 -r
  660. echo
  661. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  662. return 0
  663. fi
  664. # Create backup of main config
  665. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  666. cp /etc/syslog-ng/syslog-ng.conf /etc/syslog-ng/syslog-ng.conf.backup
  667. print_info "Backup created: /etc/syslog-ng/syslog-ng.conf.backup"
  668. fi
  669. # Copy additional config for Eye
  670. if [[ -f "/opt/Eye/docs/syslog-ng/eye.conf" ]]; then
  671. mkdir -p /etc/syslog-ng/conf.d
  672. cp /opt/Eye/docs/syslog-ng/eye.conf /etc/syslog-ng/conf.d/eye.conf
  673. # Check if conf.d inclusion already exists in main config
  674. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  675. if ! grep -q "@include.*conf\.d" /etc/syslog-ng/syslog-ng.conf && \
  676. ! grep -q "include.*conf\.d" /etc/syslog-ng/syslog-ng.conf; then
  677. # Add conf.d directory inclusion to end of file
  678. echo "" >> /etc/syslog-ng/syslog-ng.conf
  679. echo "# Include Eye monitoring configuration" >> /etc/syslog-ng/syslog-ng.conf
  680. echo "@include \"/etc/syslog-ng/conf.d/*.conf\"" >> /etc/syslog-ng/syslog-ng.conf
  681. print_info "Added conf.d directory inclusion to syslog-ng.conf"
  682. fi
  683. fi
  684. print_info "Configuration file eye.conf copied to /etc/syslog-ng/conf.d/"
  685. else
  686. print_warn "eye.conf configuration file not found in /opt/Eye/docs/syslog-ng/"
  687. fi
  688. # options block
  689. syslogng_options='options {
  690. chain_hostnames(off);
  691. flush_lines(0);
  692. use_dns(no);
  693. use_fqdn(no);
  694. dns_cache(no);
  695. owner("root");
  696. group("adm");
  697. perm(0640);
  698. stats_freq(0);
  699. time_reopen(10);
  700. log_fifo_size(1000);
  701. create_dirs(yes);
  702. keep_hostname(no);
  703. };'
  704. # Check for options in main config
  705. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  706. if ! grep -q "^options\s*{" /etc/syslog-ng/syslog-ng.conf; then
  707. # Add options block if it doesn't exist
  708. if grep -q "^@version:" /etc/syslog-ng/syslog-ng.conf; then
  709. # Insert after @version: line
  710. sed -i "/^@version:/a\\$syslogng_options" /etc/syslog-ng/syslog-ng.conf
  711. else
  712. # Insert at beginning of file
  713. sed -i "1i\\$syslogng_options" /etc/syslog-ng/syslog-ng.conf
  714. fi
  715. print_info "Added options block to syslog-ng.conf"
  716. else
  717. # Check for required parameters in existing options block
  718. local missing_params=()
  719. if ! grep -q "time_reopen\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  720. missing_params+=("time_reopen(10)")
  721. fi
  722. if ! grep -q "log_fifo_size\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  723. missing_params+=("log_fifo_size(1000)")
  724. fi
  725. if ! grep -q "chain_hostnames\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  726. missing_params+=("chain_hostnames(off)")
  727. fi
  728. if ! grep -q "create_dirs\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  729. missing_params+=("create_dirs(yes)")
  730. fi
  731. if ! grep -q "keep_hostname\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  732. missing_params+=("keep_hostname(no)")
  733. fi
  734. # Add missing parameters
  735. if [[ ${#missing_params[@]} -gt 0 ]]; then
  736. # Find options block and add parameters to end of block
  737. sed -i '/^options\s*{/,/^}/ {
  738. /^}/ i\ '"$(IFS='; '; echo "${missing_params[*]}")"';
  739. }' /etc/syslog-ng/syslog-ng.conf
  740. print_info "Added parameters to options block: ${missing_params[*]}"
  741. fi
  742. fi
  743. fi
  744. # Copy systemd service for Eye log processing
  745. if [[ -f "/opt/Eye/docs/systemd/syslog-stat.service" ]]; then
  746. cp /opt/Eye/docs/systemd/syslog-stat.service /etc/systemd/system/
  747. chmod 644 /etc/systemd/system/syslog-stat.service
  748. print_info "syslog-stat service copied"
  749. fi
  750. # Create log directory if it doesn't exist
  751. mkdir -p /opt/Eye/scripts/log
  752. chown eye:eye /opt/Eye/scripts/log
  753. chmod 770 /opt/Eye/scripts/log
  754. # Enable and start services
  755. $SERVICE_MANAGER daemon-reload
  756. if $SERVICE_MANAGER enable syslog-ng; then
  757. print_info "syslog-ng service enabled for autostart"
  758. else
  759. print_warn "Failed to enable syslog-ng for autostart"
  760. fi
  761. if $SERVICE_MANAGER restart syslog-ng; then
  762. print_info "syslog-ng service restarted"
  763. else
  764. print_warn "Failed to restart syslog-ng"
  765. fi
  766. if [[ -f "/etc/systemd/system/syslog-stat.service" ]]; then
  767. if $SERVICE_MANAGER enable syslog-stat; then
  768. print_info "syslog-stat service enabled for autostart"
  769. else
  770. print_warn "Failed to enable syslog-stat for autostart"
  771. fi
  772. if $SERVICE_MANAGER start syslog-stat; then
  773. print_info "syslog-stat service started"
  774. else
  775. print_warn "Failed to start syslog-stat"
  776. fi
  777. fi
  778. # Check syslog-ng configuration
  779. if command -v syslog-ng &> /dev/null; then
  780. if syslog-ng --syntax-only; then
  781. print_info "syslog-ng configuration is valid"
  782. else
  783. print_error "Error in syslog-ng configuration"
  784. print_warn "Check files: /etc/syslog-ng/syslog-ng.conf and /etc/syslog-ng/conf.d/eye.conf"
  785. fi
  786. fi
  787. print_info "syslog-ng configuration completed"
  788. print_info "To receive logs from devices, configure them to send to IP: $(hostname -f)"
  789. }
  790. # Configure additional services
  791. setup_additional_services() {
  792. print_step "Configuring additional services"
  793. # stat-sync service
  794. if [[ -f "/opt/Eye/docs/systemd/stat-sync.service" ]]; then
  795. cp /opt/Eye/docs/systemd/stat-sync.service /etc/systemd/system/
  796. $SERVICE_MANAGER enable stat-sync.service
  797. print_info "stat-sync service enabled"
  798. fi
  799. # eye-statd service (NetFlow)
  800. if [[ -f "/opt/Eye/docs/systemd/eye-statd.service" ]]; then
  801. cp /opt/Eye/docs/systemd/eye-statd.service /etc/systemd/system/
  802. $SERVICE_MANAGER enable eye-statd.service
  803. print_info "eye-statd service (NetFlow) enabled"
  804. fi
  805. # Configure DHCP
  806. setup_dhcp_server
  807. # Configure syslog
  808. setup_syslog
  809. }
  810. # Import MAC address database
  811. import_mac_database() {
  812. print_step "Importing MAC address database"
  813. if [[ -f "/opt/Eye/scripts/utils/mac-oids/download-macs.sh" ]]; then
  814. cd /opt/Eye/scripts/utils/mac-oids/
  815. # Download MAC database
  816. print_info "Downloading MAC address database..."
  817. bash download-macs.sh
  818. # Update vendors
  819. if [[ -f "update-mac-vendors.pl" ]]; then
  820. print_info "Updating vendor information..."
  821. perl update-mac-vendors.pl
  822. fi
  823. cd - >/dev/null
  824. else
  825. print_warn "MAC address import scripts not found"
  826. fi
  827. }
  828. # Final instructions
  829. show_final_instructions() {
  830. echo ""
  831. echo -e "${GREEN}===========================================${NC}"
  832. echo -e "${GREEN} INSTALLATION COMPLETED SUCCESSFULLY! ${NC}"
  833. echo -e "${GREEN}===========================================${NC}"
  834. echo ""
  835. echo "SYSTEM INFORMATION:"
  836. echo " Distribution: $OS_NAME"
  837. echo " Version: $OS_VERSION"
  838. echo " User: eye"
  839. echo " Directory: /opt/Eye"
  840. echo ""
  841. echo "TO COMPLETE SETUP, EXECUTE:"
  842. echo ""
  843. echo "1. Configure MySQL security:"
  844. echo " mysql_secure_installation"
  845. echo ""
  846. echo "2. Check and edit configuration files:"
  847. echo " /opt/Eye/html/cfg/config.php"
  848. echo " /opt/Eye/scripts/cfg/config"
  849. echo ""
  850. if [[ -f "/root/eye_mysql_password.txt" ]]; then
  851. echo "3. MySQL 'stat' user password saved in:"
  852. echo " /root/eye_mysql_password.txt"
  853. echo ""
  854. fi
  855. echo "4. Configure cron jobs:"
  856. echo " nano /etc/cron.d/eye"
  857. echo " Uncomment required scripts"
  858. echo ""
  859. echo "5. Configure if necessary:"
  860. echo " - DHCP: /etc/dnsmasq.conf"
  861. echo " - NetFlow: configure on network devices"
  862. echo ""
  863. echo "6. WEB INTERFACE ACCESS:"
  864. echo " URL: http://$(hostname -f)/"
  865. echo " Admin: http://$(hostname -f)/admin/"
  866. echo " Login: admin"
  867. echo " Password: admin"
  868. echo ""
  869. echo -e "${RED}IMPORTANT:${NC}"
  870. echo " - CHANGE admin password and API key!"
  871. echo " - Configure users and networks in web interface"
  872. echo ""
  873. echo -e "${GREEN}===========================================${NC}"
  874. echo ""
  875. }
  876. # Main function
  877. main() {
  878. clear
  879. echo -e "${GREEN}===========================================${NC}"
  880. echo -e "${GREEN} Installing Eye Monitoring System ${NC}"
  881. echo -e "${GREEN} for ALT Linux/Debian/Ubuntu ${NC}"
  882. echo -e "${GREEN}===========================================${NC}"
  883. echo ""
  884. # Global variables
  885. MYSQL_PASSWORD=""
  886. # Execute installation steps
  887. check_root
  888. detect_distro
  889. update_system
  890. install_packages
  891. create_user_group
  892. install_source_code
  893. download_additional_scripts
  894. setup_mysql
  895. setup_configs
  896. setup_apache_php
  897. setup_cron_logrotate
  898. setup_additional_services
  899. import_mac_database
  900. show_final_instructions
  901. }
  902. # Handle command line arguments
  903. case "$1" in
  904. --help|-h)
  905. echo "Usage: $0 [options]"
  906. echo ""
  907. echo "Options:"
  908. echo " --help, -h Show this help"
  909. echo " --auto Automatic installation (minimal interaction)"
  910. echo ""
  911. echo "Supported distributions:"
  912. echo " - ALT Linux 11.1+"
  913. echo " - Debian 11+"
  914. echo " - Ubuntu 20.04+"
  915. echo ""
  916. exit 0
  917. ;;
  918. --auto)
  919. # Mode with minimal interaction
  920. print_warn "Automatic mode. All confirmations will be accepted as 'yes'"
  921. export DEBIAN_FRONTEND=noninteractive
  922. ;;
  923. *)
  924. # Interactive mode by default
  925. ;;
  926. esac
  927. # Start installation
  928. main "$@"
  929. # Exit with success code
  930. exit 0